简体   繁体   中英

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 matters.

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM