Segmentation fault before function entry?

961 views Asked by At

UPDATE:Check my answer for details, pointer error.

I have a function, that is supposed to load a few test assets and blit them onto screen using SDL2. This function throws a segfault immediately before executing any commands, with no clear cause. Keep in mind that some of the variables in this function are globals.

Function contents:

    printf("DEBUG");
    int menuSelect = 0;
    printf("declare");
    SDL_Surface* bg = SDL_LoadBMP("menubg.bmp");
    printf("bg load");
    SDL_Surface* menu1 = TTF_RenderText_Solid(font,"HACKING PROGRAM",whiteclr);
    printf("title blip");
    SDL_BlitSurface(bg,NULL,screen,NULL);
    printf("event");
    SDL_Event* event;
    printf("menu2");
    SDL_Surface* menu2 = TTF_RenderText_Solid(font,"Hack",whiteclr);
    printf("rect");
    SDL_Rect menu2r = CreateRect(5,30,menu2->w,menu2->h);
    printf("free");
    SDL_FreeSurface(menu2);
    SDL_FreeSurface(menu1);
    while(SDL_WaitEvent(event))
    {
        switch(event->type)
        {
            case SDL_MOUSEBUTTONDOWN:
                if(event->motion.x > menu2r.x && event->motion.x < menu2r.x+menu2r.w && event->motion.y > menu2r.y && event->motion.y < menu2r.y+menu2r.h)
                {
                    SDL_FreeSurface(bg);
                    return 0;
                }
                break;
            case SDL_MOUSEMOTION:
                if(event->motion.x > menu2r.x && event->motion.x < menu2r.x+menu2r.w && event->motion.y > menu2r.y && event->motion.y < menu2r.y+menu2r.h)
                {
                    menuSelect=1;
                }
                else
                {
                    menuSelect=0;
                }
                break;
        }
        if(menuSelect==1)
        {
            menu2 = TTF_RenderText_Solid(font,"Hack",selectclr);
        }
        else
        {
            menu2 = TTF_RenderText_Solid(font,"Hack",selectclr);
        }
    }
    return 0;
2

There are 2 answers

3
Exercise To The Reader On BEST ANSWER
  1. Double check the paths on fonts, images, etc. Are they correct in relation to the root directory of the project? They are quite sensitive. To avoid simple mistakes like these: go to point 2.
  2. Apply some basic exception handling. For example:

    SDL_Surface* bg = SDL_LoadBMP("menubg.bmp");
    
    if(bg == NULL)
    {
        // This code will be run when menubg.bmp cannot be loaded.
    }
    

    It's good practice to always check the outcome of your statement to make sure it worked as planned.

  3. Use a debugger. GDB is the most popular one; but I personally use DDD, a visual debugger that is run on GDB. It's very lightweight and has both graphical and console input.
  4. Document your code. Even if it's a personal project and you're not going to show it to anyone. Explain what each statement does and why it's needed; you'll get a clearer sense of the code and you may find the problem itself.

Also, it's while(SDL_WaitEvent(&event)) not while(SDL_WaitEvent(event)).

0
jfkjsu3v95 On

This is actually a stupid error, and something that might happen to anyone who is a newbie to SDL. WaitEvent(SDL_Event*) should be fed &event, as in event should be the event, not a pointer to the event. By changing while(SDL_WaitEvent(event)) to while(SDL_WaitEvent(&event)) and changing SDL_Event* event; to SDL_Event event; this problem is fixed.

For anyone else experiencing any kind of segfault, debugging hard will do the job. The guys here at SO greatly helped me in tracing down and fixing my stupid error, and educated me as a developer.