-1

I have noticed that the destructor is not called when i'm declaring a pointer to an object when the program ends . Here is the code that I tried :

class B {
    public:
        B(){cout<<"cons B;";    };
        B(const B &b ){cout<<"copy B;"; };
       ~B(){cout<<"Destr B;";   };

 };

class Y:public B {
public:
    Y(){cout<<"cons Y;";    };
    Y(const Y &y ){cout<<"copy Y;"; };
    ~Y(){cout<<"Destr Y;";  };

};


int main(){
   Y *y= new Y;
   f(y);
}

The destructor is not called , the result is : cons B cons Y On the other hand if i run this code :

Y y;

The destructor is called , the result is : cons B cons Y dest Y dest B

My question is why is the destructor not being called in the first program ?

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
MysticJumper
  • 37
  • 1
  • 1
  • 3
  • 1
    When you create something with `new` you have to call `delete` on it to destroy it (or assign it to a smart pointer like `std::unique_ptr` which will handle the destruction automatically). – Jonathan Potter Jun 13 '19 at 20:49
  • 2
    What does your C++ textbook have to say on the subject? –  Jun 13 '19 at 20:51
  • @JonathanPotter Thank you for your answer , i have a better understanding of the subject now – MysticJumper Jun 13 '19 at 20:56
  • The destructor of `y` is called. But the destructor of a pointer doesn't do anything. That's why we have various forms of smart pointers. – Pete Becker Jun 13 '19 at 22:38

4 Answers4

1

The destructor of an object is called when the object is destroyed, not when program exits. In your code, you never destroy the object.

You have inadvertently created a memory leak.

Destroy the object when you are done with it or before exiting application and its destructor would be called.

int main(){
   Y *y= new Y;
   f(y);
   delete y;
}
zar
  • 11,361
  • 14
  • 96
  • 178
  • This answer can be improved upon by embracing modern C++ and demonstrating the firepower of a fully armed and operational `std::unique_ptr`. – user4581301 Jun 13 '19 at 21:16
  • @user4581301 good point but lets keep the answer simple to the point. The variable can just be declared on stack without pointer as well and destructor would be called but I have explained to him why its not called in his case. – zar Jun 14 '19 at 14:30
0

You have created an object of Class in heap using new operator , In heap whenever we are creating an object its our responsibility to delete the memory created dynamically. So we need to explicitly call delete to free the memory, once we use delete it will lead a call to destructor.

Chandra Shekhar
  • 598
  • 1
  • 7
  • 25
  • Or you can delegate responsibility for managing the lifetime of the dynamic allocation to a [smart pointer.](https://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one) – user4581301 Jun 13 '19 at 22:49
0

It's easy to forget that you don't have a garbage collector if you're migrating from a higher level language. A rule of thumb is that whenever you use new, use delete when you're done. A pointer will keep existing even after the function, or in your case program ends. A use for them would be a function that generates some data, a model let's say. If you use a pointer you can keep using that model after the function ends and frees its stack. Be careful with pointers and make sure that they are deleted sometime, it doesn't have to be in the same function.

A pointer is like a deposit box. You can initialize one and travel to it by dereferencing it and change its content. You can also pass the deposit box to other people and allow them to modify and see the same content as you do contrary to just giving them a copy of all the content in there. The deposit box will keep existing after the person first created it has passed since other people can use and edit its contents as well.

  • [Prefer to use a smart pointer to manually managing the lifetime of an object.](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Jun 13 '19 at 22:50
0

The destructors are called when the objects of its class are destroyed, and an object will be destroyed when:

1- when this object is an automatic object ( auto/register local object or non-static/non-extern local object) or temporary object passes out of scope in which it declared.

2- when this object is constructed external or static object and the program terminates.

3- when this object is a pointer (of its class) to "new"ly allocated memory and "delete" operator is used.

Once those objects are destroyed, the destructor of their class are called reversely.

mada
  • 1,646
  • 1
  • 15