tags:

views:

778

answers:

9

Sample

void func(void* data)
{
 CResource* resource = (CResource*)data;
 delete resource; // ~CResource never called.
 resource = NULL;
}

Kindly help me to figure out this.

+1  A: 

Destructor is called.Give us the full code.

yesraaj
+9  A: 

Summarized possible reasons why CResource destructor may be not called, extracted from other answers:

Incomplete type

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).

NULL pointer

If data is NULL, delete does nothing and does not call any destructor.

Type mismatch

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).

Suma
type information will be added with the object created on the heap when object is created with new?
yesraaj
Type information will indeed be added with the object created on the heap when object is created with new, but only if the object is of a class that has at least one virtual method.
Daniel Earwicker
Type information needed for a destructor call is a static (compile time) thing. When a type is not defined, the compiler does not know if destructors exists or not, or what the destructor is (is it virtual or not).
Suma
Wouldn't the linker take care of that? I mean, the destructor is always there (compiler generates the default one, if you don't provide one), so compiler is safe to assume, that destructor exists, and that the linker will find it. At least, that's how understand the hole compiler-linker relation...
Paulius Maruška
it's clearly stated by the std that delete on a pointer of incomplete type is undefined behavior if the complete type would have a non-trivial destructor (user-defined).
Johannes Schaub - litb
a possible problem could be caused by a private destructor, which wouldn't be spot if you don't know the definition of the class yet.
Johannes Schaub - litb
A: 

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.

dario minonne
+6  A: 

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.

Paulius Maruška
Or if data points to something other than a CResource, and CResource has virtual destructor. delete will attempt to use VMT from a 'bad' object and may randomly call something else.
Roddy
Oh, I guess I'll update my answer then.
Paulius Maruška
+2  A: 

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.

Paul Mitchell
+2  A: 
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;
}
Paul Mitchell
just a question. If the classes would have more virtual functions, and the destructor would be 1st virtual function in one class, and the 5th in the second. Would the destructor of Aardvark still get called? Or would it call one of the other virtual members?
Paulius Maruška
I just tried that with my sample. With the destructor being the first virtual function in CResource this code actually calls the first function in the vtable of Aardvark whether that is a destructor or not. I'm guessing this probably comes under the heading of 'undefined behaviour' though.
Paul Mitchell
This depends on how compiler implements this. In Visual Studio the destructor is always the first entry in the virtual function table.
Suma
Very interesting... Subtle bug's might come up, even when you're using the same class, and you changed the ordering of functions and didn't rebuild the entire project... Very interesting...
Paulius Maruška
A: 

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.

Thorsten79
A: 

please.. can you helpme? Iwant to destuctor of pointer to pointer

Post a question instead of an answer to someone else's question.
Darron
A: 

please helpme...

Ivery sad becouse the saturday exam in c++