Ariel
Ariel

Reputation: 678

destructor is never called

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

Upvotes: 3

Views: 360

Answers (3)

KedarX
KedarX

Reputation: 781

Assuming that _retainCount = 0

It works, provided you have declared your destructor virtual in base class.

See below code: (gcc version 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; 
}

Result: w/o explicit delete

A ctor
B ctor
B dtor
A dtor

Result: with explicit delete

A ctor
B ctor
B dtor
A dtor
B dtor
A dtor

Upvotes: 2

Markus Kull
Markus Kull

Reputation: 1477

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

Some good advice: Never ever build your own reference-counting scheme. Use proven library-implementations like std::auto_ptr, boost::shared_ptr, or boost::shared_array and take advantage of RAII (http://en.wikipedia.org/wiki/RAII). Also, avoid "delete this" like the plague. It may seem to work in some cases, but generally the pre/postconditions are too much.

Upvotes: 2

ereOn
ereOn

Reputation: 55796

Be sure that you declared your destructor virtual in the base class.

Upvotes: 4

Related Questions