我有以下简单的 SDL 代码:
#include <SDL.h>
#include <stdbool.h>
#include <stdio.h>
// helpers
bool init(SDL_Window **win, SDL_Surface **surf) {
int const width = 800;
int const height = 600;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
return false;
}
*win = SDL_CreateWindow("Picture test",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
width, height, 0);
if (*win == NULL) {
fprintf(stderr,
"Unable to create window: %s\n",
SDL_GetError());
return false;
}
*surf = SDL_GetWindowSurface(*win);
return true;
}
bool load_media(SDL_Surface **surf) {
*surf = SDL_LoadBMP("./sample.bmp");
if (*surf == NULL) {
fprintf(stderr, "Unable to load data: %s\n", SDL_GetError());
return false;
}
return true;
}
void close(SDL_Window **win, SDL_Surface **surf) {
SDL_FreeSurface(*surf);
SDL_DestroyWindow(*win);
SDL_Quit();
}
int main()
{
SDL_Window *win;
SDL_Surface *surf;
SDL_Surface *img;
if (!init(&win, &surf)) {
return EXIT_FAILURE;
}
if (!load_media(&img)) {
return EXIT_FAILURE;
}
SDL_BlitSurface(img, NULL, surf, NULL);
SDL_UpdateWindowSurface(win);
SDL_Delay(2000);
close(&win, &img);
}
我的代码总是在 close
时出现段错误(根据 GDB,段错误的起源是行 SDL_FreeSurface(*surf)
)。更奇怪的是,如果我用它的定义替换对 close
的调用,这仍然会在完全相同的地方出现段错误。具体来说,如果我将 close(&win, &img)
替换为:
SDL_FreeSurface(img);
SDL_DestroyWindow(win);
SDL_Quit();
代码仍然在完全相同的地方出现段错误,即使那个函数甚至没有被调用。只有当我删除整个 close
函数时,它才能正常工作。我完全不知道是什么原因造成的。
最佳答案
请重命名您的函数
void close(SDL_Window **win, SDL_Surface **surf)
因为 close
是一个标准的 C 库函数。
关于c - 来自 SDL_FreeSurface 的奇怪段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40495898/