tags:

views:

99

answers:

2

In Objective C, is there a way to determine at runtime, if an object is retained, which other object might be retaining that object?

Or to phrase it a little differently:

If there are leashes on the dog is it possible to know who is holding the leash?

Assume you have this hypothetical scenario:

MyObjectOne

and

MyObjectTwo

inside the implementation of MyObjectTwo is something like

- (void)setFirstObject:(MyObjectOne *)firstObj {
 [firstObj retain];
    // do stuff with object and under certain conditions don't release it    
}

Now elsewhere in the code there might be other places which create and retain the objects

// Create the two objects
myFirstObject = [[MyObjectOne alloc] init];   
mySecondObject = [[MyObjectTwo alloc] init];

// ...
// Some process requires retaining the first object 
[myFirstObject retain]

// ...
// some other place requires passing the first object to the second object
// at which point the first object is retained by the second object 
[mySecondObject setFirstObject:myFirstObject];

// ...
// further on the first object is released
[myFirstObject release]

At this point theoretically myFirstObject should have a retain count of 1 because it was retained inside MyObjectTwo. Is it also, possible to know WHAT is retaining the object? In other words it is possible to know that myFirstObject has a retain count of 1 AND it is currently retained by mySecondObject. Is there a convenience method for finding this information out? Is it possible to have conditional code that works like this psuedo code:

if (MyObjectTwo is retaining MyObjectOne)
{
 // do something in particular
}

Take this a few steps further and say myFirstObject has a retain count higher than 1 and that there are other objects MyObjectThree and MyObjectFour which behave similar to MyObjectTwo in that they have methods that can retain MyObjectOne. And now assume that there are multiple instances of MyObjectTwo, MyObjectThree and MyObjectFour some which are retaining the first object and some which are not. Is there a way to know who and what is doing the retaining?

Still learning, so my syntax might not be 100% correct but hopefully the question is clear.

+6  A: 

No, you can't do that. Objects do not keep track of who has retained them, and there isn't always such an object, since you can call retain from inside a plain vanilla C function, which has no associated object.

If you want to keep track of who is retaining an object, you need to do this yourself. Every time you retain, you add yourself to a list of retainers, e.g. an NSArray inside the object being retained.

I don't know exactly what you're trying to do, but you're almost certainly going about it the wrong way. Figuring out who is retaining what is not really a useful thing to know most of the time, and there's probably a much better way of doing what yo uwant.

Adam Rosenfield
Thanks for the response. I wasn't trying to do anything in particular. I was just wondering if this was theoretically possible. The concept of adding yourself to an array list of retainers makes sense.
Gordon Potter
+2  A: 

If you simply want to debug something, you can override the retain method of the class whose instance seems to get stuck in the memory. Then set a breakpoint in the overriden method and each time the breakpoint fires, you’ll see the retainer on the stack.

zoul
Makes sense. Thanks for the tip.
Gordon Potter