使用以下代码调用SDL_Quit()时遇到分段错误:
#include <SDL2/SDL.h>
#include <stdio.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int init(){
return SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
}
void close(){
SDL_Quit();
return;
}
int main(int argc, char *argv[])
{
if( init() < 0 ){
SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
SDL_Delay(1000);
close();
return 0;
}
这是gdb输出:
Breakpoint 1, main (argc=1, argv=0x7fffffffe268) at src/main.c:18
warning: Source file is more recent than executable.
18 if( init() < 0 ){
(gdb) print $eax
$1 = 4196290
(gdb) step
init () at src/main.c:8
8 return SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
(gdb) print $eax
$2 = 0
(gdb) step
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb)
然而,当我把所有东西都放在这个主要的
时int main(int argc, char* argv[])
{
if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0){
SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
SDL_Quit();
return 0;
}
我没有遇到分段错误。
你可以帮我理解这个问题。非常感谢。答案 0 :(得分:1)
好的,我发现了问题:我不得不重命名函数init()和clsoe(),因为与已定义的函数存在冲突。