segfault in InputHandler::update when moving joystick

Started by
7 comments, last by Bozemoto 4 years, 4 months ago

gdb says I.m getting a segfault in InputHandler::update when I mive the joystick, is it that the if else and for loops in the function are not seperated correctly, where do I need to add or remove french braces? I'm a complete noob and I find nested if else and for loops really hard to debug so any help is greatly appreciated. Thanks

InputHandler.cpp and all the other game engine files are included here

https://drive.google.com/open?id=1o6nLpkkKs9aafBMv1Q3AXYEmX0a_YkAX

Advertisement

This is not a Game Design question. Moving to a more appropriate forum.

-- Tom Sloper -- sloperama.com

2 hours ago, Alio said:

gdb says I.m getting a segfault in InputHandler::update when I mive the joystick, is it that the if else and for loops in the function are not seperated correctly, where do I need to add or remove french braces? I'm a complete noob and I find nested if else and for loops really hard to debug so any help is greatly appreciated. Thanks

InputHandler.cpp and all the other game engine files are included here

https://drive.google.com/open?id=1o6nLpkkKs9aafBMv1Q3AXYEmX0a_YkAX

Sorry if this is obvious, but is this your code, or is it from some other source?

For me at least the code isn't displaying with consistent indentation, which makes it difficult to analyze. If the code is under your control, that might be worth taking a look at (maybe the code is like that to begin with, or maybe it's due to a mix of spaces and tabs, but in either case it should be fixable one way or another).

It would also be helpful to identify the statement or statements that are causing the crash. I see some indexed array access, which could be an issue. Also, this may be tangential, but I'm a little skeptical of m_joystickValues storing Vector2d instances by pointer and allocating them dynamically (are they ever deleted?). That may or may not be related to the problem, but it's probably worth taking a look at regardless.

I would recommend to delete the code, and write new code all by yourself instead.

It's less fast than copying it from someone, but you will learn a lot about understanding problems and writing code, and you end up with code that you fully understand, and thus can debug. Doing that is a step in your development as a programmer.

 

New programmers often have the idea that it is all about code. That is not the case, the real thing you're doing is to get an understanding of the problem that you have such that you can explain it to a baby in excruciating detail. That baby is very fast in doing computations and understands funny artificial languages only.

The code is nothing. If you have the understanding, ie if you know how to explain it to a baby, you can literally reproduce that code in a few hours, or less depending on how fast you can type.

You need to format your code. Indentation will help debugging your curly braces.
I removed and added the curly brace to fix the indentation, and there were a few issues.
Like the Game::init method seemed a bit weirdly structured. After fixing some of those formatting errors it worked fine for me. But I was only testing with one controller.

Oh and this if(SDL_JoystickOpen(i) != 1)
SDL_JoystickOpen returns a nullptr when it fails. So change the 1 to nullptr

Also, can do this to avoid crashing when trying to use a pad 
current code


int InputHandler::xvalue(int joy, int stick)
{
    if(m_joystickValues.size() > 0)

do this


int InputHandler::xvalue(int joy, int stick)
{
    if(m_joystickValues.size() > joy)

 

Video Game Programmer.
5 years in industry.

On 11/17/2019 at 10:30 PM, Net-Ninja said:

You need to format your code. Indentation will help debugging your curly braces.
I removed and added the curly brace to fix the indentation, and there were a few issues.
Like the Game::init method seemed a bit weirdly structured. After fixing some of those formatting errors it worked fine for me. But I was only testing with one controller.

Oh and this if(SDL_JoystickOpen(i) != 1)
SDL_JoystickOpen returns a nullptr when it fails. So change the 1 to nullptr

Also, can do this to avoid crashing when trying to use a pad 
current code



int InputHandler::xvalue(int joy, int stick)
{
    if(m_joystickValues.size() > 0)

do this



int InputHandler::xvalue(int joy, int stick)
{
    if(m_joystickValues.size() > joy)

 

Can you tell me which line and file you are referring to with if(SDL_JoystickOpen(i) != 1)
SDL_JoystickOpen returns a nullptr when it fails. So change the 1 to nullptr

Please Net-Ninja? Thanks.

 

Try a search across the files for that function name, likely you get only a few hits.

I recommend using "ag" for this as it's designed for exactly this purpose, it's in the silver-searcher package.

Alternatively, "grep" can also search recursively.

1 hour ago, Alio said:

Can you tell me which line and file you are referring to with if(SDL_JoystickOpen(i) != 1)
SDL_JoystickOpen returns a nullptr when it fails. So change the 1 to nullptr

Please Net-Ninja? Thanks.

 

InputHandler.cpp line 26
It won't be the source of your problems, as the pointer is unlikely to be 1 anyway. Just something I noticed.

Video Game Programmer.
5 years in industry.

This topic is closed to new replies.

Advertisement