为什么SimpleEKDemo代码中存在内存泄漏?
分析 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您会注意到,当您运行存在泄漏的演示时,
viewDidLoad
(责任框架)中存在泄漏。如果您切换到Call Tree
详细信息并且启用了Invert Call Tree
,您将看到与调用+[NSArray new]
相关的泄漏>。如果稍微打开它,您将看到在RootViewController
的viewDidLoad
中调用的initWithArray
。问题是,eventsList 是一个保留属性,因此创建的对象的保留计数为 2。但是它仅被释放一次通过
dealloc
中的release
或通过eventsList
的重新分配。您必须自动释放该对象。一旦修复,您就不应该出现任何错误。
You will notice that when you run the demo with leaks that there is a leak in
viewDidLoad
(responsible frame). If you switch toCall Tree
detail and if you have enabledInvert Call Tree
, you will see a leak associated with the call+[NSArray new]
. If you open that a bit, you will seeinitWithArray
which is called in theRootViewController
'sviewDidLoad
. The problem bit is,eventsList
is aretain
ed property so the object created has a retain count of 2. However it is onlyrelease
d once either through therelease
indealloc
or through reassignment ofeventsList
. YOu will have to autorelease this object.Once fixed, you shouldn't get any errors.