Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Destructor called unexpectedly

Why is the destructor ~TestClass() in this simple code called twice?

#include <memory>
#include <iostream>
#include <vector>

class TestClass
{
  int m_val;

public:
  TestClass(int val);
  ~TestClass();
};

TestClass::TestClass(int val) : m_val(val)
{
  std::cout << this << " TestClass() " << m_val << "\n";
}

TestClass::~TestClass()
{
  std::cout << this << " ~TestClass() " << m_val << "\n";
  m_val = -1;
}

int main()
{
  auto x = std::make_unique<TestClass>(2);

  auto y = *x;  // <<<< this triggers it
}

Output

000001E66C0B5980 TestClass() 2
00000057FDD9FAB4 ~TestClass() 2   // why is the destructor called here?
000001E66C0B5980 ~TestClass() 2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

auto y = *x; // <<<< this triggers it

The above is copy initialization and would use copy constructor of the class. The extra destructor call that you’re seeing is corresponding to this object y. You can also verify this by adding a copy ctor to your class and you’ll notice that the copy ctor is used to create y using *x.

class TestClass
{
  int m_val;

public:
  TestClass(int val);
  ~TestClass();
   //added this copy ctor
  TestClass(const TestClass &p): m_val(p.m_val)
//-------------------------------^^^^^^^^^^^^^^--->use member initializer list
  {
      std::cout << this << " copy ctor " << m_val << "\n";
  }
};
//other code as before
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading