Why segfault in Input|Handler::update

gdb says I'm getting a segfault in InputHandler::update when I move the joystick, is it that the if else and for loops are not correctly separated, where do I need to add remove french braces? thanks.

InputHanfler.cpp and all other files of the game engine are included below.

I'm a complete noob and I find nested if else and for statements really hard to debug so any help is greatly appreciated. Thanks.

https://drive.google.com/open?id=1o6nLpkkKs9aafBMv1Q3AXYEmX0a_YkAX
Last edited on
Try this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
void InputHandler::update()
{
    SDL_Event event;
    while(SDL_PollEvent(&event))
    {
        if(event.type == SDL_QUIT)
            TheGame::Instance()->quit();

        else if(event.type == SDL_JOYAXISMOTION)
        {
            int whichOne = event.jaxis.which;

            //left stick move left or right
            if(event.jaxis.axis == 0)
            {
                if(event.jaxis.value > m_joystickDeadZone)
                    m_joystickValues[whichOne].first->setX(1);
                else if(event.jaxis.value < -m_joystickDeadZone)
                    m_joystickValues[whichOne].first->setX(-1);
                else
                    m_joystickValues[whichOne].first->setX(0);
            }

            // left stick move up or down
            else if(event.jaxis.axis == 1)
            {
                if(event.jaxis.value > m_joystickDeadZone)
                    m_joystickValues[whichOne].first->setY(1);
                else if(event.jaxis.value < -m_joystickDeadZone)
                    m_joystickValues[whichOne].first->setY(-1);
                else
                    m_joystickValues[whichOne].first->setY(0);
            }

            //right stick move left or right
            else if(event.jaxis.axis == 3)
            {
                if(event.jaxis.value > m_joystickDeadZone)
                    m_joystickValues[whichOne].second->setX(1);
                else if(event.jaxis.value < -m_joystickDeadZone)
                    m_joystickValues[whichOne].second->setX(-1);
                else
                    m_joystickValues[whichOne].second->setX(0);
            }

            //right stick move up or down
            else if(event.jaxis.axis == 4)
            {
                if(event.jaxis.value > m_joystickDeadZone)
                    m_joystickValues[whichOne].second->setY(1);
                else if(event.jaxis.value < -m_joystickDeadZone)
                    m_joystickValues[whichOne].second->setY(-1);
                else
                    m_joystickValues[whichOne].second->setY(0);
            }
        }

        else if(event.type == SDL_MOUSEBUTTONDOWN)
        {
            if(event.button.button == SDL_BUTTON_LEFT)
                m_mouseButtonStates[LEFT] = true;
            else if(event.button.button == SDL_BUTTON_MIDDLE)
                m_mouseButtonStates[MIDDLE] = true;
            else if(event.button.button == SDL_BUTTON_RIGHT)
                m_mouseButtonStates[RIGHT] = true;
        }

        else if(event.type == SDL_MOUSEBUTTONUP)
        {
            if(event.button.button == SDL_BUTTON_LEFT)
                m_mouseButtonStates[LEFT] = false;
            else if(event.button.button == SDL_BUTTON_MIDDLE)
                m_mouseButtonStates[MIDDLE] = false;
            else if(event.button.button == SDL_BUTTON_RIGHT)
                m_mouseButtonStates[RIGHT] = false;
        }
    }
}
I tried this adjustment and I'm still getting the error Segfault in InputHandler::update when i move the joystick according to gdb, is there any way i can get gdb to report the exact line of the crash? do the if else for loops need adjusting again? someone one game dev.net mentioned indexed array access and dynamically allocated pointers, where in the program should i be looking for errors regarding these areas? do i need to set up destructors cleaning up all the pointers? Thanks.
is there any way i can get gdb to report the exact line of the crash?


Build your code with debugging symbols in it. If you're building with g++ , this is done with the switch "-g" in your build command.
I noticed some errors with the < and > signs in Player.cpp handle input and the error has moved down to line 132 m_joystickValues[whichOne].second->setY(0); in InputHandler.cpp, can you spot any errors in Player.cpp handle input? I checked it with the book, if not where should I be looking for my errors, thanks.

https://drive.google.com/open?id=1ILElwa_2ZqavO_vdnbJAqzl0VJkpGFTv
Topic archived. No new replies allowed.