为什么SimpleEKDemo代码中存在内存泄漏?

发布于 2024-11-10 13:22:56 字数 351 浏览 5 评论 0原文

分析 SimpleEKDemo 应用程序时来自 Apple 我注意到存在一些内存泄漏。

其中一个泄漏是 __NSArrayM,它在泄漏块历史记录中有 3 行:Malloc/Assign/Release。

问题 - 有人可以指出这里问题的根本原因吗? (我正在尝试学习如何获取创建泄漏对象的 Instruments 输出,然后从那里找出根本原因,所以这非常有用)

When profiling the SimpleEKDemo application from Apple I note there are some memory leaks.

One of the leaks is __NSArrayM which has 3 lines in the Leaked Blocks history, a Malloc/Assign/Release.

Question - can someone point out the root cause issue here? (I'm trying learn how to take Instruments output of where a leaky object was created, and then from there work out root cause, so this would be really useful)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

画尸师 2024-11-17 13:22:56

您会注意到,当您运行存在泄漏的演示时,viewDidLoad(责任框架)中存在泄漏。如果您切换到Call Tree详细信息并且启用了Invert Call Tree,您将看到与调用+[NSArray new]相关的泄漏>。如果稍微打开它,您将看到在 RootViewControllerviewDidLoad 中调用的 initWithArray。问题是,

self.eventsList = [[NSMutableArray alloc] initWithArray:0];

eventsList 是一个保留属性,因此创建的对象的保留计数为 2。但是它仅被释放一次通过dealloc 中的release 或通过eventsList 的重新分配。您必须自动释放该对象。

self.eventsList = [[[NSMutableArray alloc] initWithArray:0] autorelease];

一旦修复,您就不应该出现任何错误。

You will notice that when you run the demo with leaks that there is a leak in viewDidLoad (responsible frame). If you switch to Call Tree detail and if you have enabled Invert Call Tree, you will see a leak associated with the call +[NSArray new]. If you open that a bit, you will see initWithArray which is called in the RootViewController's viewDidLoad. The problem bit is,

self.eventsList = [[NSMutableArray alloc] initWithArray:0];

eventsList is a retained property so the object created has a retain count of 2. However it is only released once either through the release in dealloc or through reassignment of eventsList. YOu will have to autorelease this object.

self.eventsList = [[[NSMutableArray alloc] initWithArray:0] autorelease];

Once fixed, you shouldn't get any errors.

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