destructor not called

This is a discussion on destructor not called within the C++ Programming forums, part of the General Programming Boards category; Hi,my following code..the destructor never gets called ! Code: #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; class Base{ ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    destructor not called

    Hi,my following code..the destructor never gets called !

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    class Base{
          private:
              int r;
              static int val;
          public:
                
        Base(){
            cout<<"constructing DEFAULT Base object"<<endl;
        }    
        Base(int x){
                 r=x;
            cout<<"constructing PROPER Base object-"<<r<<endl;
        }
        ~Base();
    };
    Base::~Base(){
        cout<<"destructing-"<<r<<endl;
    }    
    
    int Base::val=2; 
    
    int main(void)
    {
        Base b1(1),b2(2),b3(3);
        static Base b4(4);
        Base b5(5);
        
       
        int exit;
        cin>>exit;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,403
    Sorry, but I cannot reproduce the behaviour you described. The program prints five "constructing (...)" messages, I enter the integer input, and then it prints five "destructing (...)" messages, as expected.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    58
    oops..i think my program terminates once i input a value to exit.
    So therefore never saw my destructors.


    any way to see the destructor being called?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,403
    Quote Originally Posted by rahulsk1947
    any way to see the destructor being called?
    Run your program from the command line (e.g., open a command prompt window).

    Alternatively, you can introduce a block for local scope:
    Code:
    int main(void)
    {
        {
            Base b1(1),b2(2),b3(3);
            static Base b4(4);
            Base b5(5);
        }
    
        int exit;
        cin>>exit;
    }
    which would also help you to more clearly see the difference between the lifetime of a local and static object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by rahulsk1947 View Post
    oops..i think my program terminates once i input a value to exit.
    So therefore never saw my destructors.


    any way to see the destructor being called?
    Sure, use std::cin.get() to force the program to wait for user input. std::cin.ignore() also works

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,403
    Quote Originally Posted by legit
    Sure, use std::cin.get() to force the program to wait for user input. std::cin.ignore() also works
    That would have the same effect as the current code: the destructor messages will not be seen because they are presumably being shown on an IDE initiated command prompt that disappears very quickly after the program terminates, but if the program has not terminated, they will not be printed because the objects have not been destroyed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by laserlight View Post
    That would have the same effect as the current code: the destructor messages will not be seen because they are presumably being shown on an IDE initiated command prompt that disappears very quickly after the program terminates, but if the program has not terminated, they will not be printed because the objects have not been destroyed.
    That makes sense , I need to read more carefully from now on

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    you can set a breakpoint on the destructor.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Also, if this is a base class it would be a good idea to make the destructor virtual.

    Example:

    Code:
    struct unsafe_base
    {
    	~unsafe_base( void )
    	{
    		cout << "~unsafe_base( )" << endl;
    	}
    };
    
    struct unsafe_derived : unsafe_base
    {	
    	~unsafe_derived( void )
    	{
    		cout << "~unsafe_derived( )" << endl;
    	}
    };
    
    struct safe_base
    {
    	virtual ~safe_base( void )
    	{
    		cout << "~safe_base( )" << endl;
    	}
    };
    
    struct safe_derived : safe_base
    {	
    	~safe_derived( void )
    	{
    		cout << "~safe_derived( )" << endl;
    	}
    };
    
    int main( void )
    {
    	auto_ptr< unsafe_base >
    		u( new unsafe_derived( ) );
    	auto_ptr< safe_base >
    		s( new safe_derived( ) );
    	return 0;	
    }
    Last edited by Sebastiani; 06-17-2009 at 08:34 AM. Reason: example added
    Code:
    #include <ip.hpp>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Destructor being called on SGI hash_map key
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2009, 11:05 PM
  2. Replies: 4
    Last Post: 09-21-2008, 02:27 PM
  3. callback from exe called with system()
    By leonv in forum C Programming
    Replies: 3
    Last Post: 01-25-2008, 03:12 PM
  4. AAARG!!! mental block, what is this character called: ')'
    By compjinx in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-29-2002, 09:29 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 08:05 PM

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