What does the destructor do silently?

Posted by zhanwu on Stack Overflow See other posts from Stack Overflow or by zhanwu
Published on 2011-03-16T08:04:17Z Indexed on 2011/03/16 8:10 UTC
Read the original article Hit count: 170

Filed under:
|

Considering the following code which looks like that the destructor doesn't do any real job, valgrind showed me clearly that it has memory leak without using the destructor. Any body can explain me what does the destructor do in this case?

#include <iostream>                                                                    
using namespace std;                                                                   

class A                                                                                
{                                                                                      
private:                                                                               
    int value;                                                                         
    A* follower;                                                                       
public:                                                                                
    A(int);                                                                            
    ~A();                                                                        
    void insert(int);                                                                  
};                                                                                     

A::A(int n)                                                                            
{                                                                                      
    value = n;                                                                         
    follower = NULL;                                                                   
}                                                                                      

A::~A()                                                                             
{                                                                                   
     if (follower != NULL)                                                           
         delete follower;                                                               
     cout << "do nothing!" << endl;                                             
}                                                                                   

void A::insert(int n)                                                                  
{                                                                                      
    if (this->follower == NULL) {                                                      
        A* f = new A(n);                                                               
        this->follower = f;                                                            
    }                                                                                  
    else                                                                               
        this->follower->insert(n);                                                     
}                                                                                      

int main(int argc, char* argv[])                                                       
{                                                                                      
    A* objectA = new A(1);                                                             

    int i;                                                                             
    for (i = 0; i < 10; i++)                                                           
        objectA->insert(i);                                                            

    delete objectA;                                                                    
} 

© Stack Overflow or respective owner

Related posts about c++

Related posts about destructor