0

If I have a grandfather object that contains an array of parent objects that contains an array of children objects. Assuming I have released the objects after adding them to the arrays, how do I go about releasing all the objects? Can I just call removeallobjects on the grandfather object? When I do this I get a leak :(

Thanks

1 Answer 1

2

Simply call release on the "grandfather" object - it'll then release the "parent" objects, who will release the "children" objects, etc. all the way down. (Or "up" depending on how you look at it.)

In essence when you release an NS(Mutable)Array, it'll release the objects it has pointers to - if those objects happen to be NS(Mutable)Arrays, they'll therefore release the objects they have pointers to...

Sign up to request clarification or add additional context in comments.

4 Comments

That was my understanding to not sure why I'm getting a leak :(
Make sure that your children are not retaining their parents – that creates a retain cycle.
Yeah, this means that one or more of the objects in the graph had another, lost retain on it. That could be a cycle, or it could be just a lost retain. You can use Instruments' Allocations profile, configured to "Record Reference Counts" to see all the retains/releases that were taken on any object. It's a great tool!
@Skeep Just to let you know that @vicvicvic and @ipmcc have posted some additional comments on this answer that may prove very useful. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.