3

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

Ariel
  • 668
  • 8
  • 16
  • 1
    Are you sure the retain/release counts match? – Lothar Aug 03 '10 at 09:59
  • Does the retain count actually reach zero? Is the destructor of `CSObject` invoked? Is the destructor of `CSObject` virtual? – Mike Seymour Aug 03 '10 at 10:13
  • from debugger, 'Does the retain count actually reach zero?' yes 'Is the destructor of CSObject invoked?' yes 'Is the destructor of CSObject virtual?' yes destructor of CSArray is not invoked – Ariel Aug 03 '10 at 10:22
  • You know the best way to get a good answer is to post compilable code that reproduces the error. But lacking that it looks like either destructor of CSObject is not virtual or self is not pointing at the correct thing (what is self?). – Martin York Aug 03 '10 at 11:44
  • Could we see the declaration and definition of the destructor of `CSObject`? My money is still on that not being virtual. Also, you changed `delete this` to `delete self`. What is `self`? – Mike Seymour Aug 03 '10 at 12:00

3 Answers3

4

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

ereOn
  • 53,676
  • 39
  • 161
  • 238
2

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.

Markus Kull
  • 1,471
  • 13
  • 16
  • Studying the conditions on `delete this;` is a good idea before attempting reference counting, but reference-counting is neither complicated nor difficult, as evidenced by the HUGE number of COM components, all of which implement `IUnknown`. Also, the classes you suggest aren't thread-safe, so "reuse the library version" could make a lot of trouble. – Ben Voigt Aug 03 '10 at 12:07
  • @Ben: And OP's code is thread-safe? Also see http://stackoverflow.com/questions/692438/is-boost-shared-ptr-xxx-thread-safe – UncleBens Aug 03 '10 at 16:15
  • Markus, I like Cocoa style in, so I make them – Ariel Aug 03 '10 at 18:07
2

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
KedarX
  • 763
  • 1
  • 7
  • 15
  • I'm not quite sure what you mean by "works perfectly"; deleting an automatic variable gives undefined behaviour, and is as likely to crash as to give the double-destruction you got. – Mike Seymour Aug 03 '10 at 12:16
  • @Mike Seymour I was just to demonstrate that the destructor is called. Well I should remove word "perfectly" :) ... I didn't consider for UB here ... – KedarX Aug 03 '10 at 12:28