user1583647 user1583647 - 11 days ago 6
C++ Question

glibc detect smallbin linked list corrupted

I am trying to run a function repeatedly in a large loop but I get an error after 2 or 3 iterations but if I start from the point it gave an error it works fine but again stops after 3 4 iteration. It might be a memory problem. As the function is quite large I am not sure where exactly there is a memory leakage. Is there anyway I can free the variables after each iteration or something that can solve this problem. Or as it is a linked list memory problem free all the linked list or something?What can be the solution? The problrm does not happen if I run the function once so I think it is because I am calling the function repeatedly in a loop. Is there any way to solve this problem?

The error is

**glibc detected:.....malloc():smallbin double linked list corrupted: 0x000000000 1d404c0 ***

Answer

The library is telling you that the memory metadata is corrupt. That won't happen by mere memory leak, you had to write to invalid pointer. Either you wrote to index out of bounds or you wrote to pointer after it was freed.

The easiest way to debug this kind of issue is using valgrind. It only works under Linux, but you seem to be using that already. It is rather slow, because it single-steps the program and checks every memory-accessing instruction, but it can catch invalid memory access and also use of uninitialized variables and memory leaks very reliably.

There is also duma (detect unintended memory access) library. It can also be made to work on other platforms and is a bit faster, but it uses much more memory.

And there is gcc's own mudflap that can be activated by specific compiler options. That one should work on most gcc targets, but I am not sure how complete the C++ support is.

Comments