在Objective-C中,有没有办法找出哪些对象是另一个对象?
例如,我有一个MyClass对象的集合,如果我遍历它们以获得每个对象的保留计数,我会看到计数为2.我怎样才能找出持有者是谁?
for (NSString *aKey in aDictionaryOfMyObjects)
{
MyClassObject *myClassObj = [aDictionaryOfMyObjects objectForKey:aKey];
// following shows a retain count of 2. Presumably, the first count is
// due to myClassObj is held as the value in NSDictionary and second is because I
// I just acquired a pointer to it above. I'd like to find out who exactly
// might have references to myClassObj.
NSLog(@"retain count = %d", [myClassObj retainCount]);
}
答案 0 :(得分:3)
你做不到。您可以在-retain
上设置断点操作,以便您可以实时查看它,或者您可以使用dtrace
来实现相同的目标。但是你无法从一个物体向后移动到它的保留者。
一般来说,你不应该依赖计算保留。您应该依赖于Apple针对Objective-C的内存管理指南中编写的规则。
答案 1 :(得分:-3)
你做不到。这些信息对您来说通常不重要。