1

I encounter a segmentation fault when calling SDL_Quit() with the following code:

#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;
}

This is the gdb output:

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)  

However when I put everything in main like this

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;
}

I do not get a segmentation fault.

Could you help me to understand the problem. Thanks a lot.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Itsbananas
  • 569
  • 1
  • 4
  • 12

1 Answers1

3

Okay I found the problem: I had to rename the functions init() and clsoe() because there was a conflict with already defined function.

Itsbananas
  • 569
  • 1
  • 4
  • 12