xcode malloc:从空闲列表中出列的无效指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26870866/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:09:27  来源:igfitidea点击:

malloc: Invalid pointer dequeued from free list

c++xcodepointersmallocobjective-c++

提问by Craig

I have some C++ code in my OS X project that allocates an array thusly:

我的 OS X 项目中有一些 C++ 代码,用于分配数组:

C * p = new C[lengthHint + 2];

This is in a template class; Cis unsigned short. lengthHintis 1. This is all irrelevant. The error I get at runtime is:

这是在模板类中;Cunsigned shortlengthHint是 1. 这都无关紧要。我在运行时得到的错误是:

malloc: *** error for object 0x60800000c4f0: Invalid pointer dequeued from free list
*** set a breakpoint in malloc_error_break to debug

It appears mallocis failing because a previous call to freefreed something that wasn't valid. But it seems like freewould've complained about that at the time.

它似乎malloc失败了,因为之前调用free释放了一些无​​效的东西。但当时似乎free会抱怨。

Obviously there are millions of malloc/freeand new/deletecalls being executed and this same code is running without issues in other programs running on iOS and OS X. I'm not sure how to approach debugging this and am looking for suggestions.

显然,现在有成千上万的malloc/freenew/delete呼叫正在执行,但此相同的代码不会对iOS和OS X.我跑我不知道如何处理调试这在其他程序运行的问题,我期待的建议。

采纳答案by Craig

As I suspected, the problem didn't had anything to do with the malloccall. I had decided to ignore the problem while I worked on another issue. The project was one where I was moving some code previously written in C++ for Windows over to Mac. While changing some type names I inadvertently changed this:

正如我所怀疑的,问题与malloc通话无关。我决定在处理另一个问题时忽略这个问题。在这个项目中,我将一些以前用 C++ 为 Windows 编写的代码转移到 Mac 上。在更改某些类型名称时,我无意中更改了这一点:

TCHAR * p = new TCHAR[(length + 1)];

to this:

对此:

char * p = new char(length + 1);

So just a typo, but one with pretty significant implications.

所以只是一个错字,但具有相当重要的影响。

I discovered this while reviewing recent changes to a file that had some other odd behavior. So the answer to my original question was pretty simple and applies in a lot of other situations: "What have you changed lately?" :-)

我在查看最近对具有其他奇怪行为的文件所做的更改时发现了这一点。所以我最初的问题的答案非常简单,适用于许多其他情况:“你最近有什么变化?” :-)

回答by Andrew Redko

Probably it does not relate to your case, but wanted to share some tricky bug I've got to with "malloc: Invalid pointer dequeued from free list" error.
For me it was error in the following code:
int *array = malloc(len+1 * sizeof(int));
Since I'm newbie in C, I've missed here that malloc(len+1 * sizeof(int))incorrectly assumes C Operator Precedence.
Obviously it must be:
malloc((len+1) * sizeof(int))

可能它与您的情况无关,但想与“malloc:从空闲列表中出列的无效指针”错误分享一些棘手的错误。
对我来说,下面的代码是错误的:
int *array = malloc(len+1 * sizeof(int));
由于我是 C 的新手,我错过了这里malloc(len+1 * sizeof(int))错误地假设 C 运算符优先级。
显然它必须是:
malloc((len+1) * sizeof(int))