Objective-C 确定哪些对象保留另一个对象
我在 iPhone 上遇到了一些内存泄漏问题(想象一下),并且我有一个保留计数为 10 的自定义对象。
有什么方法可以知道哪些代码触发了特定对象实例的保留计数增加?如果这很重要的话,我正在使用 GHUnit。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用
Build &分析
。它通常可以告诉你一个对象是否被保留而不被释放。/Try using the
Build & Analyze
. It can usually tell you if an object is being retained and not released./泄漏工具(XCode 中的“工具”之一)能够分析此类事情,但我认为您不能以编程方式执行此操作。
这是一个很棒的教程: http://mobileorchard.com/ find-iphone-memory-leaks-a-leaks-tool-tutorial/
(更新以总结评论):如果您想在保留方法中设置断点(以查看堆栈跟踪),您可以重写保留方法。
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.
保留计数几乎毫无用处 - 如果某些内容在语句中被
retain
ed 和autorelease
d,那完全没问题,但其保留计数将增加 1。如果您想准确找到特定对象被
retain
编辑的位置,重写该类的retain
实现来测试您的对象,并在那里设置断点:然后运行您的程序调试器并在命中断点时查看调用堆栈。
The retain counts are nearly useless—if something gets
retain
ed andautorelease
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'sretain
implementation to test for your object(s), and set a breakpoint there: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..