-
Deleted instance
If an instance of a class is deleted with the "delete" command, doesn't it's pointer return to NULL?
If not, how am I going to find out whether an object was deleted or not? Currently I have a program where the same object is deleted twice, and both times it's checked whether the object exists or not (i.e. the pointer is not NULL), yet, ther program gives me a nasty Windows error (the detailed one with the white X on a red traffic board. Dunno what the error is called in english, as I have a Dutch Windows)
-
No, deleting a pointer does not set it to NULL.
Use something like this to safely delete your dynamic pointers:
Code:
if(ptr)
{
ptr delete;
ptr=NULL;
}
//and of course for arrays
if(ptr)
{
ptr [] delete;
ptr NULL;
}
-
You could also define a macro that doeas all that:
#define SafeDelete(Ptr) if(Ptr){delete[] Ptr; Ptr=NULL;}
So instead of calling delete[] MyPointer;
you call SafeDelete(MyPointer);
PS:
JDinger: You should have a second look at your code :)
Quote:
ptr [] delete;
ptr NULL;
-
:D thanks for the save, Magos! I'm at work so I was kind of rushing through my reply so I could take care of a phone call.
Code:
#include "Redemption.h"
//for arrays...
if(ptr)
{
delete [] ptr;
ptr=NULL
}