Malloc: Invalid pointer removed from the free list.

I have C ++ code in my OS X project that allocates an array like this:

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

This is a template class; C - unsigned short . lengthHint is equal to 1. It all does not matter. Runtime Error:

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

It seems that malloc is failing because the previous call to free freed something inappropriate. But it seems like free would be complaining about it at the time.

Obviously, millions of calls are made to malloc/free and new/delete , and the same code works without problems in other programs running on iOS and OS X. I'm not sure how to approach this debugging and am looking for suggestions.

+5
source share
2 answers

As I suspected, the problem had nothing to do with calling malloc . I decided to ignore the problem while I was working on another problem. The project was where I moved code previously written in C ++ for Windows to Mac. By changing the names of some types, I inadvertently changed this:

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

:

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

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

I discovered this by looking at recent changes to a file with other odd behavior. Therefore, the answer to my initial question was quite simple and applicable in many other situations: "What have you changed lately?" :-)

+4
source

This probably does not apply to your case, but I would like to share some complex error that I encountered with the malloc: Invalid pointer dequeued from free list error.
For me, it was an error in the following code:
int *array = malloc(len+1 * sizeof(int));
Since I'm new to C, I skipped here that malloc(len+1 * sizeof(int)) incorrectly assumes C Operator Priority.
Obviously, this should be:
malloc((len+1) * sizeof(int))

0
source

Source: https://habr.com/ru/post/1206661/


All Articles