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

发布于 2024-10-01 11:31:01 字数 119 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千纸鹤 2024-10-08 11:31:01

尝试使用Build &分析。它通常可以告诉你一个对象是否被保留而不被释放。/

Try using the Build & Analyze. It can usually tell you if an object is being retained and not released./

甜警司 2024-10-08 11:31:01

泄漏工具(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.

瑾兮 2024-10-08 11:31:01

保留计数几乎毫无用处 - 如果某些内容在语句中被 retained 和 autoreleased,那完全没问题,但其保留计数将增加 1。

如果您想准确找到特定对象被 retain 编辑的位置,重写该类的 retain 实现来测试您的对象,并在那里设置断点:

@implementation MyClass
-(id) retain
{
    if(self == ObjectThatImTracking)
        NSLog(@"[ObjectThatImTracking retain]\n");  // put a breakpoint here
    return [super retain];
}

然后运行您的程序调试器并在命中断点时查看调用堆栈。

The retain counts are nearly useless—if something gets retained and autoreleased 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 retained, 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.

少女七分熟 2024-10-08 11:31:01

您是否试图在模块中查找您的班级的所有保留案例?马比它有帮助..

Did you trying to find all retain cases of your class in modules? Maby it helps..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文