Jump to content
  • Advertisement
Sign in to follow this  
digitalfreak

when is destructor automatically called?

This topic is 5093 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

on page 307 of the C++ Programming Language (special edition), he says when an object is destroied, first itself is destroied, then its members, and then the base. does this mean when I delete the object, its member's destructor is automatically called? I tried in the code but it was not called.

Share this post


Link to post
Share on other sites
Advertisement
If you have members that are pointers, you need to delete those yourself. You also need to make sure your base class's destructor is virtual. If the members are themselves derived objects, then you need to make sure the destructors of their base classes are also virtual, etc.

Share this post


Link to post
Share on other sites
maybe the member function is declared on the heap ? (with a new statement)
=> add a corresponding delete in the destructor

edit: never mind, see above

Share this post


Link to post
Share on other sites
Note that when it mentions that members are destroyed, it's referring to static (non-pointer) members. Member pointers are not automatically deleted, you have do this yourself in the destructor.


class MyClass {
SomeType staticObj; // this will be automatically destroyed

SomeType* dynObj; // you have to delete this yourself,

~MyClass() {
if (dynObj) // like this
delete dynObj;
}
};


Share this post


Link to post
Share on other sites
Hmm, I'm not sure I completely understand your question, and, I'm not sure that even if I did, I should be in the position of explaining this, but here goes nothing anyway :)

I'm assuming you have, say, a pointer to an object as a member of a base object, and, when the base object is destroyed, you want the child objects to be deleted as well? Provided that is what you are asking, here is my meagre contribution:

An objects destructer is called on a few occasions, the ones I know are, firstly, when an object goes out of scope, or secondly, when a pointer to an object is 'delete'ed. If you have an object, with pointers to other objects, the other objects will not be destroyed when you destroy the base object, because, although you can no longer access them, the (child) objects still exist in memory. So what you have to do is, in the destructer of the base object, you have to 'delete' all the member pointers, to ensure they are destroyed (whoa, almost said 'destructed' there :P) when the base object is destroyed.

However, if, in turn, you mean in a case like this:

class B;

class A
{
public:
A();
~A();
B myMemberClass;
};




And when you destroy the A object, the destructer of B is not called, well, then, I have no idea what is going wrong, as by all rights it SHOULD be?

[EDIT] Wow, 4 posts while I type [smile] New record for me [grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by ajas95
If you have members that are pointers, you need to delete those yourself


But only delete them if the class is the owner of the data!

Share this post


Link to post
Share on other sites
Morbo - It is unnecessary to test for NULL-ness. Deleting a NULL pointer is a no-op, never an error.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Morbo - It is unnecessary to test for NULL-ness. Deleting a NULL pointer is a no-op, never an error.


agreed. and you should get in the habit of setting poitner to NULL once you've released them. It will always save you debugging time!!

Cheers
Chris

Share this post


Link to post
Share on other sites
Hi, thanks for all you replies. here's my design:

first I have a struct BitmapInfo for hold bitmap informations I read from a .bmp file, in it there's a UCHAR *data field for holding the actual image data, which is dynamically newed once the bitmap's image data's length is read out.
BitmapInfo has a destructor which simply delete[] data.

then I have a Cube class which has a member of type BitmapInfo (not pointer), and Cube doesn't have a desctructor.

in my program I first new a Cube and then use it (including fill its BitmapInfo member by loading a .bmp file) and lastly delete it, but by setting break point I could tell that BitmapInfo's desctructor was not called during Cube's delete.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!