从教程网站获取的以下基本SDL2代码正在引起一些奇怪的麻烦:
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define SCREENH 768
#define SCREENW 1366
SDL_Window *window = NULL;
SDL_Surface *screenSurface = NULL;
SDL_Surface *windowSurface = NULL;
int init_SDL() {
int success = 0;
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! ");
printf("SDL_Error: %s\n",SDL_GetError());
success = -1;
}
else {
window = SDL_CreateWindow("SDL2_Tutorial02",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREENW,SCREENH,SDL_WINDOW_SHOWN);
if(window == NULL) {
printf("Window could not be created! ");
printf("SDL Error: %s\n",SDL_GetError());
}
else {
screenSurface = SDL_GetWindowSurface(window);
}
}
return success;
}
int loadMedia() {
int success = 0;
windowSurface = SDL_LoadBMP("Images/Hallo.bmp");
if(windowSurface == NULL) {
printf("Unable to load image! ");
printf("SDL Error: %s\n",SDL_GetError());
success = -1;
}
return success;
}
void close() {
SDL_FreeSurface(windowSurface);
windowSurface = NULL;
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
}
int main(int argc,char *argv[]) {
assert(init_SDL() == 0);
assert(loadMedia() == 0);
SDL_BlitSurface(windowSurface,NULL,screenSurface,NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(3000);
close();
exit(EXIT_SUCCESS);
}一旦调用SDL_Quit() (放置在close()中),我就会收到一个内存访问错误。通过使用GDB,揭示了以下情况:
49 SDL_Quit();
(gdb) n
Program received signal SIGBUS, Bus error.
0x00007ffff68a5895 in ?? () from /usr/lib/x86_64-linux-gnu/libX11.so.6
(gdb) 奇怪的是,当我将SDL_Quit()放在close()之外时,如下所示:
void close() {
SDL_FreeSurface(windowSurface);
windowSurface = NULL;
SDL_DestroyWindow(window);
window = NULL;
}
int main(int argc,char *argv[]) {
assert(init_SDL() == 0);
assert(loadMedia() == 0);
SDL_BlitSurface(windowSurface,NULL,screenSurface,NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(3000);
close();
SDL_Quit();
exit(EXIT_SUCCESS);
}一切都很好。SDL_Quit()工作时没有错误。当我在另一个函数中调用SDL_Quit()时,它为什么会导致SIGBUS错误?
编辑:这段代码是在ubuntu14.04上用gcc编译的,下面是编译命令
gcc -g3 -o tutorial tutorial.c `sdl2-config --cflags --libs` 发布于 2015-10-06 18:10:04
您的函数close()与具有相同名称的内部SDL函数冲突,导致了奇怪的行为(实际上,它是SDL调用的libc标准close() syscall )。
重新命名您的函数,它应该是好的。
https://stackoverflow.com/questions/32976279
复制相似问题