title: Objective-C: how to tell which other object is retaining a specific object?
body: <p>In Objective C, is there a way to determine at runtime, if an object is retained, which other object might be retaining that object?</p>
  
  <p>Or to phrase it a little differently:</p>
  
  <p>If there are leashes on the dog is it possible to know who is holding the leash?</p>
  
  <p>Assume you have this hypothetical scenario:</p>
  
  <p>MyObjectOne</p>
  
  <p>and </p>
  
  <p>MyObjectTwo</p>
  
  <p>inside the implementation of MyObjectTwo is something like</p>
  
  <pre><code>- (void)setFirstObject:(MyObjectOne *)firstObj {
   [firstObj retain];
      // do stuff with object and under certain conditions don't release it    
  }
  </code></pre>
  
  <p>Now elsewhere in the code there might be other places which create and retain the objects</p>
  
  <pre><code>// 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]
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>if (MyObjectTwo is retaining MyObjectOne)
  {
   // do something in particular
  }
  </code></pre>
  
  <p>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?</p>
  
  <p>Still learning, so my syntax might not be 100% correct but hopefully the question is clear.</p>