Thread: Destructors & exit(0)

  1. #1
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140

    Destructors & exit(0)

    Will the destructor for a class execute if I use the exit(0); at a random place in the program?
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    That's a bad idea to use exit() when you have classes.

  3. #3
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    i'm wondering if it's neccessary to call a function that deletes all the dynamic stuff before exiting(0)
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    I believe that exit() destroys static members, but does not call the destructors, so I would think you should do something to destroy all of the dynamic stuff before exit()

  5. #5
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    Thankee so much.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    going further into this topic, if int main() ended by return or it reaches the end bracket, are destructors called then? or does the programmer need to call destructors manually all the time?

  7. #7
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    i think the destructors are called if the object wasn't declared as pointers and then new'ed.

    if they were new'edpointers, then they'd have to be deleted.

    i'm not 100% on this, though.

  8. #8
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    destructors are called when an instance of the class goes out of scope. (just like any other variable)
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    The Dog:
    If your object creates dynamic instances of some other object, you would have your destructor do the 'deleting' of those created objects...

    destructors are an automation, there's nothing tricky about them

  10. #10
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    Will the destructor for a class execute if I use the exit(0); at a random place in the program?
    Destructors are called whenever an object is destroyed, either explicitly (MyClass::~MyClass) or implicitly (move out of scope). The exit function will destroy all static (non-dynamic) variables and classes, calling the destructors as required. Return from main() is the same as exit. Using abort() will immediately exit the program without calling destructors.
    i think the destructors are called if the object wasn't declared as pointers and then new'ed. if they were new'edpointers, then they'd have to be deleted. i'm not 100% on this, though.
    True. For dynamically created objects, the destructor will not be called unless the object is specifically deleted. If a pointer moves out of scope, the pointer itself is destroyed but not the object it points to. If your program exits without destroying objects on the heap, those objects will not be destroyed and the memory will not be returned to the system. It is lost; the OS will not reallocate that memory. More correctly, there is no guarantee that the memory will be returned to the OS. Most OS's are pretty good about cleaning up after a program, but you shouldn't depend on it.

    fletch
    "Logic is the art of going wrong with confidence."
    Morris Kline

  11. #11
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    While fletch is correct in saying that all static objects will be destructed. auto objects (normally those created on the stack - not with new) will not necessarily be destructed. Therefore if you've allocated within these objects you may have a memory leak.

    I believe you will be better off avoiding exit() and finding a mechanism to allow your program to die naturally.

  12. #12
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    While fletch is correct in saying that all static objects will be destructed. auto objects (normally those created on the stack - not with new) will not necessarily be destructed. Therefore if you've allocated within these objects you may have a memory leak.
    Static objects are automatically deleted, but doesn't the compiler clean up the stack as part of exit()? Hmmm...wait...the keyword there is compiler - not something that you can always rely on. Hence "will not necessarily be destructed." Don't you love it when you answer your own question?

    I believe you will be better off avoiding exit() and finding a mechanism to allow your program to die naturally.
    That is a fact.

    fletch
    "Logic is the art of going wrong with confidence."
    Morris Kline

  13. #13
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    hehe, well at least we have firmly agreed that static objects will be destroyed

  14. #14
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    thankyou all, that clears it up.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Destructors
    By $l4xklynx in forum C++ Programming
    Replies: 15
    Last Post: 02-19-2009, 09:47 PM
  2. Destructors in STL?
    By sawer in forum C++ Programming
    Replies: 4
    Last Post: 08-09-2006, 09:35 AM
  3. Destructors in dynamically allocated arrays
    By frenchfry164 in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2003, 11:26 PM
  4. Constructors and Destructors
    By GravtyKlz in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2003, 10:44 AM
  5. Virtual & Pure virtual destructors
    By BMJ in forum C++ Programming
    Replies: 61
    Last Post: 08-22-2002, 09:38 AM