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.