Unhandled exception at ......
-
Hi all. I have this code:
//an object, create as MObject* object = new MObject(); object->value=10; //try to delete to delete memory of this object delete object; //and then check it //this object is not NULL if (object!=NULL){ //its memory is deleted, so below command is error, how to catch this error by //**try{}catch("what is here"){}**, or have other way to catch this error? int j=object->value; ........ }
Please help me. Thanks -
Hi all. I have this code:
//an object, create as MObject* object = new MObject(); object->value=10; //try to delete to delete memory of this object delete object; //and then check it //this object is not NULL if (object!=NULL){ //its memory is deleted, so below command is error, how to catch this error by //**try{}catch("what is here"){}**, or have other way to catch this error? int j=object->value; ........ }
Please help me. Thanks -
Hi all. I have this code:
//an object, create as MObject* object = new MObject(); object->value=10; //try to delete to delete memory of this object delete object; //and then check it //this object is not NULL if (object!=NULL){ //its memory is deleted, so below command is error, how to catch this error by //**try{}catch("what is here"){}**, or have other way to catch this error? int j=object->value; ........ }
Please help me. ThanksDeleting an object doesn't invalidate the pointer to the object. You need to do: delete object; object=null; I'm not sure how to check if deleting of the object actually worked...anyone...anyone..
- S 50 cups of coffee and you know it's on!
-
Shuang. Wu wrote:
try{ int j = object->value; } catch(...){ // handling here }
Thanks, but this does not work. I using VC++ 2005.
-
Deleting an object doesn't invalidate the pointer to the object. You need to do: delete object; object=null; I'm not sure how to check if deleting of the object actually worked...anyone...anyone..
- S 50 cups of coffee and you know it's on!
Steve Echols wrote:
You need to do: delete object; object=null;
Thanks, but if i do this code. The program does bot call:
int j=object->value;
and the error does not occur.:) Above is a simple example, my problem is very complicate and i can not set NULL to object. Thanks again. -- modified at 2:55 Friday 14th April, 2006 -
Steve Echols wrote:
You need to do: delete object; object=null;
Thanks, but if i do this code. The program does bot call:
int j=object->value;
and the error does not occur.:) Above is a simple example, my problem is very complicate and i can not set NULL to object. Thanks again. -- modified at 2:55 Friday 14th April, 2006Maybe I'm misunderstanding your problem... If you delete an object, you can no longer access that object. I.e. if you do: delete object; You are not allowed to access any member of the object, since it is "gone" (i.e. object->value will return something you don't expect). If you do: delete object; object=null; And then try to access object->value, you will most likely get an access violation (which is correct). This is correct: [By correct, I mean the code works, but don't do it this way, since you know you just deleted the object.] delete object; object=null; if (!object) { // the object is deleted, you can't access it } else { int j = object->value; } Does that make sense?
- S 50 cups of coffee and you know it's on! -- modified at 3:06 Friday 14th April, 2006
-
Maybe I'm misunderstanding your problem... If you delete an object, you can no longer access that object. I.e. if you do: delete object; You are not allowed to access any member of the object, since it is "gone" (i.e. object->value will return something you don't expect). If you do: delete object; object=null; And then try to access object->value, you will most likely get an access violation (which is correct). This is correct: [By correct, I mean the code works, but don't do it this way, since you know you just deleted the object.] delete object; object=null; if (!object) { // the object is deleted, you can't access it } else { int j = object->value; } Does that make sense?
- S 50 cups of coffee and you know it's on! -- modified at 3:06 Friday 14th April, 2006
Steve Echols wrote:
This is correct: delete object; object=null; if (!object) { // the object is deleted, you can't access it } else { int j = object->value; }
Yes, if i do that my problem will be fixed. But this code is a simple problem. my problem complicate than that, and i don't permission to set
object = NULL;
i just have permission to access it and check it for existing or deleted. So if you know how to check it for existing(not deleted) please give me that code.try{}catch("what is here"){}
or other way? Thanks again. -
Steve Echols wrote:
This is correct: delete object; object=null; if (!object) { // the object is deleted, you can't access it } else { int j = object->value; }
Yes, if i do that my problem will be fixed. But this code is a simple problem. my problem complicate than that, and i don't permission to set
object = NULL;
i just have permission to access it and check it for existing or deleted. So if you know how to check it for existing(not deleted) please give me that code.try{}catch("what is here"){}
or other way? Thanks again.From the code from your first post, you have permission to delete the object, but not set it to null? That's strange. I'd have to see some more code to see what's going on....
- S 50 cups of coffee and you know it's on!
-
In general
catch(...)
is bad form. A general rule of exception handling is that you should only catch what you expect to be thrown. Steve