C++ SDL2 Programm crashes on switch loop

0

I ran in some trouble by "learning" SDL2. The program just crashes with the "errormodulename" StackHash_0a9e (whatever THAT is), whenever I press a key which is in my switch loop. This is the method:

 void InputMan::acceptInput(SDL_Event * e,Graphics * g){
std::cout<<"handling input"<<std::endl;
switch(e->key.keysym.sym){
    case SDLK_UP:
    {
    Sprite * s=g->spriteByName("Filip");
    if(s->getRow()==2){
        s->action();
    }
    else{
        s->setRow(2);
        s->rollBack();
    }
    std::cout<<"Key up"<<std::endl;
    break;
    }
    case SDLK_DOWN:
    {
        Sprite * s=g->spriteByName("Filip");
        if(s->getRow()==0){
            s->action();
        }
        else{
            s->setRow(0);
            s->rollBack();
        }
        std::cout<<"Key down"<<std::endl;
        break;
    }
    case SDLK_LEFT:
    {
        Sprite * s=g->spriteByName("Filip");
        if(s->getRow()==1){
            s->action();
        }
        else{
            s->setRow(1);
            s->rollBack();
        }
        std::cout<<"Key left"<<std::endl;
        break;
    }
    case SDLK_RIGHT:
    {
        Sprite * s=g->spriteByName("Filip");
        if(s->getRow()==4){
            s->action();
        }
        else{
            s->setRow(4);
            s->rollBack();
        }
        std::cout<<"Key right"<<std::endl;
        break;
    }
    default:
     break;
    }
 }

I thought that the cause could be, that there is something in the cases, which is forbidden in c++ and I just didn't know that, but with the output-debugmethod I found out that the crash is imediately after the break. Maybe the problem isn't really me and the SDL but me and C++.

EDIT

The debugger says :

 Program received signal SIGSEGV, Segmentation fault.
 0x00000190 in ?? ()

(if I press the right arrow button. on left it's 0x00000064 on up 0x000000c8 and on down 0x00000000) Apparently I have a reference which is pointing on an invalid area.

EDIT 2:

@Jarod has gave me the hint, that maybe the Sprite (Sprite * s=g->spriteByName("Filip");) is an nullptr but it seems, that it doesn't. I added an if-statement:

 Sprite * s=g->spriteByName("Filip");
    if(s==NULL){
        starter.die("Sprite \"Filip\" not found");//<-closes the program with an error message)
    } 

But the statement was not triggered.

INFO

I don't know if I already said that but the crash comes AFTER the method is completed and BEFORE the next. That is the reson, why I don't posted the main class here, but I think I should...

 void Starter::gameLoop(){
 std::cout<<"Entering Gameloop"<<std::endl;
 while(!quit){
    SDL_PollEvent(ev);
    if(ev->type==SDL_QUIT){
        quit=true;
        std::cout<<"successfull end"<<std::endl;
    }
    else if(ev->type==SDL_KEYDOWN){
        input->acceptInput(ev,graphics);//<-- the method I posted already
        std::cout<<"Paint"<<std::endl;//<-- the program crashes before that happens
    }
    else if(ev->type==SDL_KEYUP){
        graphics->spriteByName("Filip")->rollBack();
    }
    graphics->paint();
 }
}

I hope YOU can help me to fix that.

t h a n k   y o u .

c++
crash
switch-statement
sdl-2
asked on Stack Overflow Aug 2, 2014 by A13XI5 • edited Aug 3, 2014 by A13XI5

2 Answers

1

Don't declare ev as a pointer. And declare SDL_Event e inside of the gameLoop method, and then pass it to another functions by value or by reference, it's always done like this in tutorials and books:

void InputMan::acceptInput(SDL_Event e,Graphics * g){...}

or

void InputMan::acceptInput(SDL_Event &e,Graphics * g){...}

and use it in your game loop like this:

void Starter::gameLoop()
{
    std::cout<<"Entering Gameloop"<<std::endl;

    while(!quit)
    {

    SDL_event e;
    SDL_PollEvent(&e);

    switch(e.type)
    {
     case SDL_QUIT:
       quit=true;
       std::cout<<"successfull end"<<std::endl;
       break;

     case SDL_KEYDOWN:
       input->acceptInput(e,graphics);
       std::cout<<"Paint"<<std::endl;
       break;

     /*another cases*/
    }
    }
}

But the problem may be in your graphics class instance. It may be not initialized properly. Always check them on NULL like troyane said, and try to debug all way down to the crash place, it may give you some answers. And I highly encourage you to use smart pointers provided by C++ 11 standard, like shared_ptr, instead of raw pointers.

answered on Stack Overflow Aug 3, 2014 by Russel Ledge • edited Aug 3, 2014 by Russel Ledge
0

Be careful using pointers. Your function's arguments are SDL_Event * e, Graphics * g, so before accessing pointers check if it is not NULL. Since, every time you try to dereference NULL-pointer you got crash.

answered on Stack Overflow Aug 2, 2014 by tro

User contributions licensed under CC BY-SA 3.0