Thread: destructor not called

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    838
    you can set a breakpoint on the destructor.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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 <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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-11-2009, 12:05 AM
  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, 04: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, 10: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, 09:05 PM