Sample
void func(void* data)
{
CResource* resource = (CResource*)data;
delete resource; // ~CResource never called.
resource = NULL;
}
Kindly help me to figure out this.
Sample
void func(void* data)
{
CResource* resource = (CResource*)data;
delete resource; // ~CResource never called.
resource = NULL;
}
Kindly help me to figure out this.
Summarized possible reasons why CResource destructor may be not called, extracted from other answers:
One possible cause is you have the CResource type only declared, not defined:
class CResource;
void func(void* data)
{
CResource* resource = (CResource*)data;
delete resource; // ~CResource never called.
resource = NULL;
}
This is an undefined behaviour (deleting incomplete type). In a case like this compiler should issue a warning about destructor not called (Visual C++ definitely does issue it). If this is the case, make sure you have the type defined at the place where you are destructing it (include required headers).
If data is NULL, delete does nothing and does not call any destructor.
If CResource destructor is virtual and the object stored on memory pointed to by data is actually a different type, you get an undefined behavior. Often a different destructor will be called (if the object has another virtual destructor), in other situations the program may crash (if the object has no virtual destructor).
Why are you saying dtor is not called? Probably because you added a printf in your dtor, and you're not seeing any message?
Probably another dotr is invoked? Is CResource inherited? Di you define the base class destructor virtual?
As rajKumar is pointing out, give us the full code and we'll try to help you.
The only reason, why the destructor wouldn't be called, is if the data pointer is 0 (or NULL). That's how delete works - it checks if the pointer isn't 0, and if it isn't - it calls the necessary destructors and releases the memory.
As pointed out in the comment. There is another reason why it wouldn't get called. If data points to some other class (not CResource) object and both classes have virtual destructors, then the destructor of that other class will be called.
Is the destructor virtual? Maybe data doesn't point to a CResource object at all and the virtual destructor of some other class is being called.
class Aardvark
{
public:
virtual ~Aardvark()
{
printf("Aardvark::~Aardvark\n");
}
};
class CResource
{
public:
virtual ~CResource()
{
printf("CResource::~CResource\n");
}
};
void func(void* data)
{
CResource* resource = (CResource*)data;
delete resource; // ~CResource never called.
resource = NULL;
}
int _tmain()
{
void *data = new Aardvark();
func( data );
return 0;
}
I assume CResource is a superclass of some sort for you to allow easy transfer of objects via generalized pointers.
Then your question is a normal one for C++ newbies, and you should read
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7
Additionally, get a copy of Scott Meyer's "Effective C++" because it will introduce you to a lot of errors you will be making the next months.