Destructor is never called

sorry for my inattension, in short, ~CSArray()this is work, but the interface and implementation of the class were in different files, so the error is here

+3
source share
3 answers

Make sure you specify your destructor virtualin the base class.

+4
source

The code shown is currently too short to see the problem.

: . -, std:: auto_ptr, boost:: shared_ptr boost:: shared_array RAII (http://en.wikipedia.org/wiki/RAII). , " ", . , , / .

+2

, _retainCount = 0

, virtual .

. : (gcc 3.4.3)

#include<iostream>
using namespace std;

class A
{
    public:
        A(){cout<<"A ctor"<<endl;};
        virtual ~A(){cout<<"A dtor"<<endl;};
        void testDel()
        {
            delete this;
        }
};

class B: public A
{
    public:
        B(){cout<<"B ctor"<<endl;};
        ~B(){cout<<"B dtor"<<endl;};
};

int main()
{
    B bObj;

    bObj.testDel(); 
   return 0; 
}

:

A ctor
B ctor
B dtor
A dtor

:

A ctor
B ctor
B dtor
A dtor
B dtor
A dtor
+2

Source: https://habr.com/ru/post/1757799/


All Articles