开发者

destructor is never called

sorry for my inattension, in short the ~CSArray() is work, but interface 开发者_如何学Pythonand implementation of the class there was in different files, so mistake is here


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


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜