Objective-C: how to determine which other object is storing a specific object?

In Objective C, there is a way to determine at runtime if an object is saved, what other object can save that object?

Or tell this a little bit:

If the dog has leashes, is it possible to find out who is holding the leash?

Let's assume you have this hypothetical scenario:

MyObjectOne

and

MyObjectTwo

inside the implementation of MyObjectTwo there 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 may be other places that create and save 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, in theory, myFirstObject should have a persistence rate of 1 because it was stored inside MyObjectTwo. Is it also possible to know WHAT the object is storing? In other words, you can find out that myFirstObject has a persistence value of 1, and it is currently persisted by mySecondObject. Is there a convenient method for finding this information? Is it possible to have a conditional code that works like this psuedo code:

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

      

Take this a few more steps and say that myFirstObject has a retention rate above 1 and that there are other objects MyObjectThree and MyObjectFour that behave similarly to MyObjectTwo, since they have methods that can persist MyObjectOne. And now suppose there are multiple instances of MyObjectTwo, MyObjectThree, and MyObjectFour that persist the first object and some do not. Is there a way to find out who is saving what?

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

+2


source to share


2 answers


No, you cannot do this. Objects don't keep track of who saved them, and there isn't always such an object, since you can call retain

from a simple vanilla C function that doesn't have an object associated with it.

If you want to keep track of who is saving an object, you need to do it yourself. Every time you save, you add yourself to the holder list, for example. a NSArray

inside the stored object.



I don't know exactly what you are trying to do, but you will almost certainly be wrong. Figuring out who is saving what is actually not very useful to know most of the time, and is probably a much better way to do what you need to do.

+6


source


If you just want to debug something, you can override a method of a retain

class whose instance seems to be stuck in memory. Then set a breakpoint in the overriden method, and every time the breakpoint is hit, you will see a commit on the stack.



+6


source







All Articles