iOS Objec-C 错误:未分配正在释放的对象指针
我在 xcode 中收到以下错误。
对象 0x4e18d00 的错误:未分配被释放的指针 ** 在 malloc_error_break 中设置断点进行调试
我已在目标中设置 NSZombieEnabled,以便可以查看调用。它是
-[___NSArrayI release]
看起来我已经在代码中的其他地方释放了一些数组,然后自动释放池在它已经被释放时也尝试释放它。
我怎样才能找到哪里?有什么想法吗?
仅供参考,我使用 arrayWithCapacity 方法或类似方法创建所有数组,切勿使用 alloc 或 init 方法。我没有看到我在任何地方释放这些相同的数组。 (也许我是瞎子!!)
另外,控制流程如下:我单击一个UIButton
,触发附加到onclick的函数。这将进入各个逻辑层,然后返回一个 NSArray
。然后我可以在“onClick 按钮函数”中迭代该数组并打印内容。 当这个“onClick 按钮函数”退出时,我在“main”方法中收到上述错误。
另一个注意事项是,在一个函数中,我创建了一个 NSMutableArray,但想要返回一个 NSArray,因此我使用了 [[mutableArray copy] autorelease]。这样可以吧?
任何建议将不胜感激,因为我经常很难尝试追踪错误的原因。
提前致谢。
I'm getting the following error in xcode.
error for object 0x4e18d00: pointer being freed was not allocated ** set a breakpoint in malloc_error_break to debug
I've setup NSZombieEnabled in the target so I can view the call. It is
-[___NSArrayI release]
It looks like i've released some array somewhere else in my code and then the auto release pool is trying to release it also when it is already dealloc'd.
How can I find out where? Any idea?
FYI, I'm creating all my arrays using the arrayWithCapacity
method or something similar, never use alloc
or init
methods. I don't see, anywhere where I am releasing those same arrays. (maybe i'm blind!!)
Also, the control flow is as follows: I click an UIButton
, fire the function attached to onclick. This will go to various logic layers and then get an NSArray
back. I can then iterate this array in the "onClick button function" and print the contents.
When this "onClick button function" quits I get the above error in the "main" method.
Another note is that in one function I create an NSMutableArray but want to return an NSArray
so I use [[mutableArray copy] autorelease]
. This ok, right?
Any advice would be greatly appreciated as I often have great difficulty in trying to track the cause for errors down.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现出了什么问题。
在书本类中,我将章节声明为 NSAray,在默认构造函数中,我说
我将其从构造函数中取出,一切都很好。
谢谢你们的帮助。
PS 如果我忘记了,有人可以将此标记为已接受的答案吗?干杯;)
I figured out what was wrong..
In book class I had chapters declared as an NSAray and in the default constructor I said
I took that out of the constructor and all is well.
Thanks for your help guys.
P.S. If I forget can somebody please mark this as the accepted answer? Cheers ;)
当您的应用程序崩溃时,您可以查看应用程序在哪一行崩溃,还可以查看崩溃发生之前调用的函数序列。我认为这条线导致崩溃[[mutableArray copy] autorelease]。因为你没有保留它。 copy 返回一个自动释放的对象。您可以在文档中看到该函数返回的内容。在 Objective-C 中通常返回一个自动释放的对象。
When your app crashes you can see at which line your app crashed and you can also see sequence of functions called before crash happen. I think this line is causing crash [[mutableArray copy] autorelease]. because you are not retaining it. copy returns an autoreleased object. You can see in documentation that what the function is returning. In objective-c normally returns an autoreleased object.