SDL简单线程产生分段错误

时间:2016-09-18 11:55:05

标签: c segmentation-fault cygwin sdl mingw-w64

我一直在尝试使用SDL线程,但我无法使其正常工作。即使是最简单的程序也会导致分段错误。

我在cygwin环境中使用mingw64(不是mingw32)。 此代码在cygwin内部和外部运行时都会产生分段错误:

#include <SDL.h>
#include <SDL_thread.h>

int threadFunction(void* data) {
    return 0;
}

int main(int argc,char* argv[]) {
    int dataIn=0;
    int dataOut=0;
    SDL_Thread* thread;
    SDL_Window* window;

    SDL_Init(0);
    thread=SDL_CreateThread(&threadFunction,"Thread",&dataIn);
    SDL_WaitThread(thread,&dataOut);
    SDL_Quit();
    return 0;
}

但是,一旦在其间添加了printf语句,就像这样:

SDL_Init(0);
printf("#1 ");
thread=SDL_CreateThread(&threadFunction,"Thread",&dataIn);
printf("#2 ");
SDL_WaitThread(thread,&dataOut);
printf("#3 ");
SDL_Quit();
printf("#4 ");
return 0;

发生了一件非常奇怪的事。当此代码在cygwin环境中运行时,无论是否有-mwindows选项,它都可以正常工作。该程序正常退出。当这个相同的代码在外部的cygwin环境中运行时,它只会在没有-mwindows选项的情况下崩溃 。重新排列printf的结果会产生不同的结果,有时会使其崩溃,有时甚至不会崩溃。

运行此代码时,输​​出为:

Cygwin:  ./test -> #1 #2 #3 #4 
Windows: test   -> #1 (program freezes and a "test.exe has stopped working" window appears)

我使用的编译命令是:gcc -I./SDL2 -o test test.c -lmingw32 -lSDL2main -lSDL2其中gcc是mingw64 C编译器的x86_64版本的符号链接。我也尝试过使用正常的x86(i386,32位)版本的mingw64 / SDL,同样的事情发生了。

使用-g选项进行编译并在第一个示例(不使用printf&#39; s)上使用gdb时,会显示:

Starting program: /Test/test
[New Thread 5828.0x1084]

Program received signal SIGSEGV, Segmentation fault.
0x000000006c868e30 in ?? () from /Test/SDL2.dll
(gdb) where
#0  0x000000006c868e30 in ?? () from /Test/SDL2.dll
#1  0x000000006c830d29 in SDL_LogCritical ()
   from /Test/SDL2.dll
#2  0x000000006c7cb85f in SDL_LogCritical ()
   from /Test/SDL2.dll
#3  0x0000000000401600 in SDL_main (argc=1, argv=0x2f0010) at test.c:17
#4  0x0000000000402cfa in console_main ()
#5  0x0000000000402db1 in WinMain ()
#6  0x00000000004013e8 in __tmainCRTStartup ()
    at /usr/src/debug/mingw64-x86_64-runtime-4.0.6-1/crt/crtexe.c:332
#7  0x000000000040151b in mainCRTStartup ()
    at /usr/src/debug/mingw64-x86_64-runtime-4.0.6-1/crt/crtexe.c:212

而printf版本:

Starting program: /home/Mimi/Escocia/C/Test/test
[New Thread 3380.0x98c]
[New Thread 3380.0x1a80]
[Thread 3380.0x1a80 exited with code 0]
#1 #2 #3 #4 [Inferior 1 (process 3380) exited normally]

(但在cygwin之外运行此程序会产生段错误)

在mingw64上链接libmingw32错了吗? (它似乎也不对,但没有它就没有编译。)我错过了一些明显的东西吗?有人知道我做错了什么吗?这是一个错误吗? (如果是,我应该在哪里报告:SDL,mingw64或Cygwin?)

提前致谢。

编辑:重新编译SDL后,问题就消失了。有谁知道它为什么现在有效?自己编译它有什么不利之处吗?

0 个答案:

没有答案
相关问题