Help with retain/release

Help with retain/release
0.0 0



I am running into an issue trying to release() an Array that I had previously retained with retain()

Basically I have an Array* member variable that contains a number of Sprite* objects that are children of some other Node.

At some point I want to destroy the array and create a new one. I was seeing that the Sprites were not being freed (destructor not called) until the program exited, basically because the parent Node still had a reference to them. So before releasing the Array I looped over each Sprite and called sprite->getParent()->removeChild(sprite, true);

But when I do that, if I try and call release() on my array, I run into an assertion failure:
“Assertion `_reference > 0’ failed.”

Do I not need to call release() on my array once the contained Sprite objects have been removed from their parent Node? I am seeing the destructors for contained Sprite objects called… I am just not clear how the reference to my array is being removed since I had called retain().

Thanks for any clarification anyone can provide


Have you added your Array to any object as a child?


I am having a similar issue if anyone can help clarify


No, it is only stored as a member variable, not added as part of the Node hierarchy.

I can see that by calling retainCount() immediately after calling retain() on it, the value is 2 as I would expect since the instantiation of it should set it to 1, and retain() increment by one.
Subsequent message loops, retainCount() returns 1, also what I would expect (the reference I created with retain()).

But it looks like when I loop over the array and release all the objects it contains, the array itself is also being released.


Hmm… sounds weird. Probably, you should try to replicate this issue in the small isolated project and share the code if you don’t manage to figure out the root of the problem.