Search code examples
c++multithreadingsdl

Multithreaded SDL error in C++


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?


Solution

  • 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.