简体   繁体   English

Objective-C确定哪些对象保留另一个对象

[英]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. 我在iPhone上的内存泄漏有一些问题(可以想象),并且我有一个保留计数为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. 如果这很重要,我正在使用GHUnit。

Try using the Build & Analyze . 尝试使用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. 泄漏工具(XCode中的“工具”之一)能够分析这种事情,但是我认为您不能以编程方式来做到这一点。

Here is a great tutorial: http://mobileorchard.com/find-iphone-memory-leaks-a-leaks-tool-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. (更新以总结注释):如果您想在keep方法中设置一个断点(以查看堆栈跟踪),则可以覆盖keep方法。

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. 保留计数几乎是无用的-如果某条语句在语句中被retainautorelease ,那是完全可以的,但是其保留计数将增加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: 如果要确切地找到要retain特定对象的位置,请重写该类的retain实现以测试您的对象,并在此处设置断点:

@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.. Maby可以帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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