开发者

C++ Ref Count Changes While Destructing Object

开发者 https://www.devze.com 2023-02-09 00:16 出处:网络
I\'ve got 开发者_开发问答a private ref count inside the class SharedObject. SharedObject is a base class for other classes, for example Window. Window is the base class of Editor.

I've got 开发者_开发问答a private ref count inside the class SharedObject. SharedObject is a base class for other classes, for example Window. Window is the base class of Editor.

When the ref count reaches 0, because of calling SharedObject::Release(), the SharedObject deletes itself. First we get to the Editor destructor, which shows that the this pointer contains m_refs == 0, but when we get to the Window destructor it is suddenly 1, and when we reach the SharedObject destructor, it is still 1.

I put a breakpoint on the SharedObject::IncRef() method, and it was never called while this happened.

What the?


Build with optimizations off, and set a memory breakpoint on your m_refs.


Seems like you have memory leak somewhere, maybe even long before this destruction occurs. I use Alleyoop to find leaks. Can help, won't hurt to have that out of the way.

Do you use multiple threads? Maybe it's due to some raw pointer somewhere being grabbed by other thread during destruction.

On a side note, I recommend using boost::intrusive_ptr - very convinient pattern for handling addrefs and releases in shared objects that helps to be consequent with it, but this probably won't solve your problem unless you have a real mess in your code ;)

0

精彩评论

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

关注公众号