简体   繁体   English

C ++中的多线程SDL错误

[英]Multithreaded SDL error in C++

I'm building a program in C++, using SDL, and am occasionally receiving this error: 我正在使用SDL在C ++中构建程序,偶尔会收到此错误:

* glibc detected * ./assistant: double free or corruption (!prev) *检测到glibc * ./assistant:双重释放或损坏(!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 . 线程不共享任何变量,尽管它们都运行函数SDL_BlitSurfaceSDL_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? 如果这是原因,那么我是否应该在所有SDL调用周围抛出互斥体?

are you running with the MALLOC_CHECK_ environment variable set? 您是否在设置MALLOC_CHECK_环境变量的情况下运行? 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. 这会打开glibc中的内存检查,并且由于glibc内存检查代码( http://sourceware.org/bugzilla/show_bug.cgi?id=10282 )中的竞争条件,我以前遇到了问题。虚假地发布这样的消息。 Try running under valgrind and see if that sees any issues. 尝试在valgrind下运行,看看是否发现任何问题。

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. 我没有终止它们与main的关系,而是让他们在看到main完成运行(通过全局“运行”变量)后返回,问题消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM