ios malloc:对象 0x165060 的 *** 错误:未分配被释放的指针?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12049058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
malloc: *** error for object 0x165060: pointer being freed was not allocated?
提问by hacker
I have an application in which i have some videos and audios and some inapp purchases.all r great in simulator and working perfectly.But yesterday i have created an application and trying to run on it its crashing from the begning.The error report is
我有一个应用程序,其中有一些视频和音频以及一些应用程序内购买。所有 r 在模拟器中都很棒并且运行良好。但是昨天我创建了一个应用程序并尝试在其上运行它从一开始就崩溃了。错误报告是
malloc: *** error for object 0x165060: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
can anybody knows the solution .i dont know where it is going wrong and in simulator it is working perfectly.can anybody help me?
任何人都可以知道解决方案。我不知道哪里出了问题,在模拟器中它运行良好。有人可以帮助我吗?
回答by talkol
You are probably releasing an object too many times (for example, calling alloc once and release twice). To find out where, take a look at the techniques in this question: How to find the cause of a malloc "double free" error?
您可能多次释放对象(例如,调用 alloc 一次并释放两次)。要找出在哪里,请查看此问题中的技术:如何找到 malloc “double free”错误的原因?
I personally like the NSZombieEnabled
method.
我个人喜欢这种NSZombieEnabled
方法。
Another tip, is to set your variables to nil after you release them.
另一个技巧是在释放变量后将它们设置为 nil。
For example: [bla release]; bla = nil;
例如: [bla release]; bla = nil;
This makes sure you will not accidentally release them twice since releasing nil
does nothing.
这确保您不会意外释放它们两次,因为释放nil
什么也不做。
回答by Avin
I followed as talkol suggested
我按照talkol建议的
In my case I replaced following line
在我的情况下,我替换了以下行
[myMutualArray removeAllObjects];
[myMutualArray removeAllObjects];
with
和
[myMutualArray removeAllObjects]; myMutualArray = nil;
And the error gone!
错误消失了!
回答by Eshwar Chaitanya
Please test the program for memory leaks,also check autoreleases and whether you are releasing objects properly or not.Also we need to check whether a released object has a memory allocated or not.You also need to be careful regarding autorelease,because accidentally we might release an array or a string or any object that is already autoreleased...
请测试程序是否存在内存泄漏,同时检查自动释放以及您是否正确释放对象。此外,我们还需要检查释放的对象是否已分配内存。您还需要注意自动释放,因为我们可能会不小心释放一个数组或字符串或任何已经自动释放的对象...
Here are some of the tips to trace out the exact problem:
以下是一些可以找出确切问题的提示:
You can test for leaks by analyzing your project(click shift+command+k)
Use instruments tool i.e. running for leaks
Enable NSZombie in Xcode,procedure can be found here
您可以通过分析您的项目来测试泄漏(点击 shift+command+k)
使用仪器工具,即运行泄漏
在 Xcode 中启用 NSZombie,程序可以在这里找到
Hope it helps and works!
希望它有所帮助并有效!
回答by Sven
Without seeing any code we can't help you with this. But you can find the problem yourself, the error message even tells you what to do: set a breakpoint on malloc_error_break
and you usually will find the code that caused the problem in the stack trace.
没有看到任何代码,我们无法帮助您。但是你可以自己找到问题,错误信息甚至会告诉你该怎么做:设置一个断点malloc_error_break
,你通常会在堆栈跟踪中找到导致问题的代码。
Just because it works in the simulator just doesn't mean that your code is 100% correct. some bugs only show up on a real device (or vice versa).
仅仅因为它在模拟器中工作并不意味着您的代码是 100% 正确的。一些错误只出现在真实设备上(反之亦然)。
回答by Kris Roofe
回答by Gralha
I got this kind of error when your trying to call free on a pointer that the address is not at start of block. For example:
当您尝试在地址不在块开头的指针上调用 free 时,我遇到了这种错误。例如:
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6
7 int a; // some integers
8 int *pi; // a pointer to an integer
9 pi =(int*) malloc( 1*sizeof(int) );
10
11 a = 5;
12 pi = &a; // pi points to a
13 pi++; // pi is not anymore at start
14 free(pi);
15
16 printf("at the end, we are alived \n"); // this will not be printed
17
18 return 0;
19 }