Destructors not called?

This is a discussion on Destructors not called? within the C++ Programming forums, part of the General Programming Boards category; Code: Destructor object1( 1, "global object" ); int main() { cout << "\n\nENTER MAIN FUNCTION\n"; Destructor object2( 2, "local automatic" ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    115

    Destructors not called?

    Code:
    Destructor object1( 1, "global object" );
    
    int main()
    {
        cout << "\n\nENTER MAIN FUNCTION\n";
        Destructor object2( 2, "local automatic" );
        static Destructor object3( 3, "static object" );
        create();
        cout << "\nEXIT MAIN FUNCTION\n";
         
        cin.get();
        return 0;
    }
    void create()
    {
       cout << "\nCREATE FUNCTION IS CALLED" << endl;
       Destructor object4( 4, "local automatic for create function" );
       static Destructor object5( 5, "static object for create function" );
       cout << "\nEXIT CREATE FUNCTION\n";
    }
    The output is:
    Code:
    Object 1 global object is created
    
    
    MAIN FUNCTION
    Object 2 local automatic is created
    Object 3 static object is created
    
    CREATE FUNCTION IS CALLED
    Object 4 local automatic for create function is created
    Object 5 static object for create function is created
    
    EXIT CREATE FUNCTION
    Object 4 local automatic for create function is destructed
    
    EXIT MAIN FUNCTION
    Destructors are called automatically when an object goes out of scope, I understand that the destructors are called in reverse order when the objects are created. The code above is provided by the book, the output from the book is showing that all the objects created are destructed ( displaying Object blah..blah..blah is destructed) but as you can see in my output the only object destroucted that I can explicitly see is the Object 4. Where are the others?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It gets destroyed when you hit the end of main (after your cin.get()) - are you actually running your code from a command prompt, or running from an IDE that closes the windom immediately after it finishes - if so, you may not SEE the destructor call's printout.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    If that output which you captured is while your program is waiting on the cin.get(), then the output is correct. Object 2 will be destructed at the end of main, and I believe all static objects are destructed at the same time, if not then sometime after main ends but before the process is cleared out. Try removing the cin.get(), recompiling, then running the program from an open console window so it does not close when the program exits...
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    matsp ninja points++
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    lol, I never thought of that.. such a small problem caused a lot of confusion.. Thanks everybody..

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by matsp View Post
    It gets destroyed when you hit the end of main (after your cin.get()) - are you actually running your code from a command prompt, or running from an IDE that closes the windom immediately after it finishes - if so, you may not SEE the destructor call's printout.

    --
    Mats

    I'm using DEVC++, I understand now what happened. I tried to run the pogram in a command prompt and I saw the result.. thanks so much to you all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-21-2008, 02:27 PM
  2. when is the constructor of a global object called??
    By mynickmynick in forum C++ Programming
    Replies: 6
    Last Post: 08-28-2008, 04:57 AM
  3. Functions which return a reference
    By Stonehambey in forum C++ Programming
    Replies: 10
    Last Post: 07-05-2008, 01:43 PM
  4. How keep constructor from being called till "ready"?
    By 6tr6tr in forum C++ Programming
    Replies: 7
    Last Post: 04-23-2008, 01:20 AM
  5. Multiple inheritance; destructors
    By Boksha in forum C++ Programming
    Replies: 3
    Last Post: 07-19-2006, 02:50 PM

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