tags:

views:

123

answers:

2

Hi when I was trying to execute my program(c++) i was getting the following error:

a.out: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted

and when i traced my program using cout's, I could find that, it is because of the following line

BNode* newNode=new BNode();

If i remove this line I was not getting the error.

Can any one please help in this regard...

+3  A: 

The shown line of code is ok in general. The heap probably was corrupted before. I would use a memory checker like valgrind to find out where.

Without a memory checking tool you just have to look hard at your code and find the error.

Sometimes a binary search strategy helps. Deliberately deactivate parts of your code and narrow down. Don't be fooled by false positives like the line you posted.

Another alternative is to switch to a programming language with automatic memory management.

Peter G.
may i know the reason for getting this error and is there any other way to remove this error without using such tools..
siri
@siri: It is caused by something corrupting the data used by malloc to keep track of memory allocations. The most likely causes are writing beyond the bounds of an allocated block, and writing to a block after it has been freed. These errors can be difficult to track down without a memory checker; I'd strongly advise you to use one. In C++, the best way to avoid the second error is never to manage memory yourself; always use smart pointers, containers, and other management objects.
Mike Seymour
+1 for valgrind - or electric fence
pm100
+1  A: 

The error message means that the integrity of the program heap was violated. The heap was broken. The line you removed... maybe it was the culprit, maybe it was not to blame. Maybe the heap was damaged by some code before that (or even well before that) and the new that you removed simply revealed the problem, not caused it. There's no way to say from what you posted.

So, it is possible that you actually changed nothing by removing that line. The error could still be there, and the program will simply fail in some other place. Buffer overrun, double free or something like that is normally to blame for the invalidated heap. Run your code through some static or dynamic checker to look for these problems (valgrind, coverity etc.)

AndreyT