Objective-C determine what objects retain another object
I have some problems with memory leaks on iPhone (imagine that), and I have a custom object with a retain count of 10.
Is there any way I can know what code triggered the retain count increased for a specific object instance? I am using GHUnit if that mat开发者_运维知识库ters.
Try using the Build & Analyze
. It can usually tell you if an object is being retained and not released./
The leaks tool (one of the "instruments" in XCode) is able to analyse that sort of thing, but I don't think you can do it programatically.
Here is a great tutorial: http://mobileorchard.com/find-iphone-memory-leaks-a-leaks-tool-tutorial/
(Update to summarise comments): If you'd like to set a breakpoint in the retain method (to look at the stack trace) you can override the retain method.
The retain counts are nearly useless—if something gets retain
ed and autorelease
d in a statement, that's perfectly fine, but its retain count will increase by 1.
If you want to find exactly where a particular object is being retain
ed, override the class's retain
implementation to test for your object(s), and set a breakpoint there:
@implementation MyClass
-(id) retain
{
if(self == ObjectThatImTracking)
NSLog(@"[ObjectThatImTracking retain]\n"); // put a breakpoint here
return [super retain];
}
Then run your program in the debugger and look at the call stack when the breakpoint gets hit.
Did you trying to find all retain cases of your class in modules? Maby it helps..
精彩评论