Destructor called on a Sprite child when autorelease


I have an empty derived Sprite class.
I addChild to it another Sprite class I created (lets call it ChildSprite).

When I create the ChildSprite object, I do childSprite->autorelease();

However as soon as the program starts, the destructor of childSprite is called and I get an assert message “Node still marked as running on node destruction! Was base class onExit() called in derived class onExit() implementations?”

If I remove the autorelease, everything works.

Any ideas why ?

Show us more code for each class.

I think it’s because Ref::autorelease() decrements the reference count. It was previously 1, and now it will be 0. Thus this ChildSprite instance will be marked to be destroyed after you call this method, which yields that message, since it’s running yet marked to be destroyed.

Why did you call Ref::autorelease() if you do not intend to destroy the instance?

1 Like

Ref::autorelease() does not change ref count, it just adds specified object into autorelease pool. When ever this pool is cleared it also destroy the objects which were added into this pool.
Use autorelease pool only for objects that you created using new operator, If you are using Sprite::Create(...) to create sprite and added that a child to another node, you won’t need to call autorelease.

1 Like