Destruction

This is a discussion on Destruction within the C++ Programming forums, part of the General Programming Boards category; Regarding destructors, does it work the same as a constructor whereby if you define part of it yourself then the ...

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438

    Destruction

    Regarding destructors, does it work the same as a constructor whereby if you define part of it yourself then the default is ignored and only what you write is initialised?
    so is there a chance of a memory leak if i say have an object containing some big arrays as well as image surfaces and then in the destructor i only explicitly free the memory for the surfaces?
    or will everything be freed anyway?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    If you don't write any code to explicitly do something, then of course it won't just happen automagically. That's why RAII is so important - by delegating the task to a "smart" object, you generally don't have to write much "clean-up" code in your destructors.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    The destructor of every object in the class will of course be called in the default destructor. However, as you should know, there is no destructor for built-in types such as pointers, hence they will not release the memory they hold.
    And since there is only one destructor (a destructor cannot take any parameters), it's either the default or your own.
    So you must explicitly call delete in your destructor to free any pointers, or alternatively use a smart pointer whose destructor will release any pointer it holds.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Just thought I'd add that in inheritance heirarchies, the destructor of any base classes will always be called. The one exception is that if you allocate a derived object to a base-class pointer, and then delete the latter, the chain of destructors will only be called if the base class declares it's destructor as virtual, so be sure to do that always, obviously.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    cheers, i checked up on a mailing list article in response to your replies, its got plenty of good stuff about 'auto' freeing memory, in the present application i think that i can keep track easy enough and will be sure, now that i know to write all the cleanup stuff in, one thing i did spot in the article was a link to exception handling and it says if you wrap the program in a try...catch.... does this mean that in my case with an events driven loop in main() i can wrap that in a try.. catch and direct exceptions for handling, for everything?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    For the general case, yes, you can do that. In some situations, though, you'll want to handle certain exceptions elsewhere in the program's logic. It just depends, really.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Temporary object destruction
    By Mortissus in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2006, 07:47 AM
  2. An Idea for Mass Destruction
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 05-04-2006, 04:00 PM
  3. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  4. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  5. weapons of mass destruction
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 128
    Last Post: 07-18-2003, 02:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21