Code: Select all
#include <iostream>
using namespace std;
class ref
{
public:
int refCount;
ref() : refCount(1)
{}
void release()
{
if (--refCount == 0)
delete this;
}
~ref()
{
cout << "ref destructor" << endl;
}
};
class foo : public virtual ref
{
public:
~foo()
{
cout << "foo destructor" << endl;
}
};
int main()
{
foo* example = new foo();
example->release();
}
On MSVS2010 this piece of code crashes after printing out "ref destructor" with an invalid block type (debug assertion). It's the same thing as if you tried to call delete on a pointer to an object that was already deleted. It seems to me like the destructors are getting called in the reverse order.
So I guess my question is what's happening with Irrlicht's reference counters, because the code would point towards a crash (by my logic), but as we all very well know, Irrlicht does not go around crashing every time it reaches a ref count of 0 on some object? Could someone find what's wrong with this brainfart or point me towards some code that's taking care of this?