views:

69

answers:

2

I'm building a program in C++, using SDL, and am occasionally receiving this error:

* glibc detected * ./assistant: double free or corruption (!prev)

It's difficult to replicate, so I can't find exactly what's causing it, but I just added a second thread to the program, and neither thread run on its own seems to cause the error.

The threads don't share any variables, though they both run the functions SDL_BlitSurface and SDL_Flip. Could running these concurrently throw up such an error, or am I barking up the wrong tree?

If this is the cause, should I simply throw a mutex around all SDL calls?

Thanks

+1  A: 

are you running with the MALLOC_CHECK_ environment variable set? This turns on memory checks in glibc, and I've had problems with it before because of a race condition in the glibc memory checking code (http://sourceware.org/bugzilla/show_bug.cgi?id=10282) which made it put out messages like this spuriously. Try running under valgrind and see if that sees any issues.

Chris Card
A: 

Turns out that it was being caused by the threads not terminating correctly. Instead of terminating them from main, I allowed them to return when they saw that main had finished running (through a global 'running' variable), and the problem disappeared.

wyatt
The solution is most probably not this, but how you were terminating the threads in the code that you have removed.
David Rodríguez - dribeas