1

This is just suppose to display a bmp image to the SDL window front buffer. I played around with the code. And I think there is something wrong with my init() function. I'm new to SDL. But there must be a problem with my pointers or something I'm missing about SDL's fucntions EDIT: I used GDB and it turned out my close() function was the problem. I believe it was because I was freeing memory that was set to NULL? I got rid of the close fucntion and just freed mem after my delay function.

#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdbool.h>
#define SCREENWIDTH 640
#define SCREENHEIGHT 480
SDL_Window *win = NULL;
SDL_Surface *scrn = NULL;
SDL_Surface *mscrn = NULL;
bool init()
{
   bool suc = true;
   char name[11] = "Hello SDL";
   if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    printf("%s", SDL_GetError());
    suc = false;
   } 
  win = SDL_CreateWindow(name, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREENWIDTH, SCREENHEIGHT, SDL_WINDOW_SHOWN);
  if (win == NULL) {
    printf("%s", SDL_GetError());
    suc = false;
  }
scrn = SDL_GetWindowSurface(win);

return suc;
}   
bool loadmedia()
 {
   bool suc = true;
   mscrn = SDL_LoadBMP("hello_world.bmp");
   if (mscrn == NULL) {
   printf("%s", SDL_GetError());
   suc = false;
  }
   return suc;
} 
void close()
{
  SDL_FreeSurface(mscrn);
   SDL_DestroyWindow(win);
   SDL_Quit();
}
int main(int argc, char* args[])
{
 if (!init()) {
   close();
   return 1;
  } 
  if (!loadmedia()) {
   close();
   return 1;
  } 
   SDL_BlitSurface(mscrn, NULL, scrn, NULL);
   SDL_UpdateWindowSurface(win);
   SDL_Delay(3000);

   close();
   return 0;
}
ZhangBing
  • 45
  • 1
  • 11
  • 1
    What does your compiler tell you about `close()`? – EOF Jan 30 '17 at 22:20
  • What `close` is supposed to do, anyway? – Eugene Sh. Jan 30 '17 at 22:20
  • Wild guess: If SDL_Init fails, you message (and never see it because there is no trailing newline and thus no implicit flush), but then march on and try to create a window anyway. Likewise with the windows surface. A *debugger* is on the menu at this point. – WhozCraig Jan 30 '17 at 22:21
  • My close() function turned out to be the problem. I wanted to free mscrn after I was done with it and destroy a window. I think the problem was if init() is false then win would be set to NULL and msrcn would still be set to NULL aslo? But it seems my biggest problem with this was not using a debugger – ZhangBing Jan 30 '17 at 22:49
  • 1
    @ZhangBing `close` is a problem only because of its name. There is a standard libc function with that name, which closes file descriptor, and your function overrides it, but does completely different things. During `SDL_Init` SDL may use file descriptors to e.g. connect to X11 server (via network socket), queries some information, and calls `close` on that descriptor, which in turn calls your `close` instead - but at this point window and renderer are not created yet. Don't override system functions. Rename it or mark it `static`. Also you shoud not free window surface. – keltar Jan 31 '17 at 06:19

1 Answers1

2

You should find a reasonable debugger and other tools to to find out which line is causing the error and why. Basically it boils down to using a debugger which usually comes with your IDE if you're using one, or using the very good code analysis tool, Valgrind.

If you're using gcc you can likely use gdb to debug your program easily. Here are some resources on how to help you diagnose segmentation faults:

Get familiar with these tools, as they will save you countless hours in the future when you face new problems.

Janne Enberg
  • 1,908
  • 1
  • 14
  • 12