Archived

This topic is now archived and is closed to further replies.

[C++] Access members during destructor

This topic is 5401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have come across a situation where I have a class who''s members I wish to be able to access, even while it is being deleted (ie. it''s still executing the destructor, so the data isn''t gone yet). Here''s an example. Let''s say I have a class named foo, and for whatever reason, it has a member variable named checkme. Another class, bar, has a function, TestCheck(). Now, an instance of bar has a pointer to an instance of foo. This instance of foo has a pointer to the instance of bar that has a pointer to this instance of foo. During the destruction of foo, it calls ptrbar->TestCheck(); TestCheck() checks the value of ptrfoo->checkme and does something based on this value. Is this legal? Can TestCheck() access foo while foo is in the destruction portion of its existence? The code in question compiles under VC++ 6, but I have no easy means of testing whether it works or not right now, and I''m uneasy about whether it''s actually allowed under the C++ standard. In case your wondering how I got into such a situation, foo and bar are actually the same class, and there is a parent child relationship occuring the needs to be updated if one or the other is deleted.

Share this post


Link to post
Share on other sites
perfectly fine

don''t be mulled into thinking that deletion changes the state of an object, When you use the delete operator on a point, the pointer calls the destructor for that object, and THEN it deletes it. so until the destructor has finished execution, you can do whatever you like with the memory space.

Also note: that space is still there even after destruction, but the OS doesn''t know that you expect to use it. So you can STILL access data in deleted objects before the next new operation is called (after which point you can''t guarantee that another object hasn''t been allocated into the same spot.

George D. Filiotis

I am a signature virus. Please add me to your signature so that I may multiply.

Share this post


Link to post
Share on other sites
The only thing to be careful about, is to make certain the dtor cannot throw an exception. If it can throw an exception and it does so during the stack-unwinding while handling another exception, it''s a no-win situation and abort is invoked, terminating your application.

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
The only thing to be careful about, is to make certain the dtor cannot throw an exception. If it can throw an exception and it does so during the stack-unwinding while handling another exception, it''s a no-win situation and abort is invoked, terminating your application.


I was also taught to not call virtual function in destructors. Does anybody knows if this is correct, and why?

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
The only thing to be careful about, is to make certain the dtor cannot throw an exception. If it can throw an exception and it does so during the stack-unwinding while handling another exception, it''s a no-win situation and abort is invoked, terminating your application.


I was also taught to not call virtual function in destructors. Does anybody knows if this is correct, and why?

Share this post


Link to post
Share on other sites
Well, kinda. Check it out: Suppose you have class A, and class B which subclasses class A. When an object of class B is destroyed, first the destructor for B gets called. It cleans up whatever it needs to. Then the destructor for A is called. If A tries to use a virtual function that B has overloaded, the function will be called, but it will be operating on (possibly) invalid or deallocated data. So if you have a subclass that is not itself going to be subclassed, it''s okay for it to call virtual functions, since no destructors have executed before it. But any classes that can be subclassed should not.


But... but that''s what HITLER would say!!

Share this post


Link to post
Share on other sites