RESOLVED:segfault error; help please?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
rolland
Chaos Rift Regular
Chaos Rift Regular
Posts: 127
Joined: Fri Dec 21, 2007 2:27 pm
Current Project: Starting an Android app soon
Favorite Gaming Platforms: PS1, N64
Programming Language of Choice: C++
Location: Michigan, US

RESOLVED:segfault error; help please?

Post by rolland »

Maybe it's because I've been devving all night, but this is beyond me... Whenever I run my project, it gets to the constructor and calls updateStates(). It segfaults when trying to access the controller, whether it comes to the hat, axes, or buttons first.

typedef'd struct called myController...

Code: Select all

typedef struct
{
    Sint16 axis[4];
    bool button[12];
    Uint8 hat;
} myController;
class header called inputClass

Code: Select all

class inputClass
{
    private:
        SDL_Joystick* joystick;
        myController* controller;
    public:
        inputClass();
        ~inputClass();
        void updateStates();
        myController* getStates();
};
source code

Code: Select all

inputClass::inputClass()
{
    SDL_Init(SDL_INIT_JOYSTICK);
    joystick = SDL_JoystickOpen(0);
    //std::cout << "joystick open" << std::endl;
    updateStates();
}

void inputClass::updateStates()
{
    for(int i = 0; i < 4; i++)
    {
        controller->axis[i] = SDL_JoystickGetAxis(joystick, i);
    }
    for (int i = 0; i < 12; i++)
    {
        controller->button[i] = SDL_JoystickGetButton(joystick, i);
    }
    controller->hat = SDL_JoystickGetHat(joystick, 0);
}
Last edited by rolland on Sun Jan 11, 2009 1:11 pm, edited 1 time in total.
I'll write a signature once I get some creativity and inspiration...
User avatar
Amarant
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Wed Nov 05, 2008 9:52 am

Re: segfault error; help please?

Post by Amarant »

The 'controller' pointer has probably not been initialized.
But to sidestep that problem you could just have this:

Code: Select all

class inputClass
{
    private:
        SDL_Joystick* joystick;
        myController controller; // value instead of pointer
    public:
        inputClass();
        ~inputClass();
        void updateStates();
        myController* getStates();
};
and this

Code: Select all

void inputClass::updateStates()
{
    for(int i = 0; i < 4; i++)
    {
        controller.axis[i] = SDL_JoystickGetAxis(joystick, i);
    }
    for (int i = 0; i < 12; i++)
    {
        controller.button[i] = SDL_JoystickGetButton(joystick, i);
    }
    controller.hat = SDL_JoystickGetHat(joystick, 0);
}
and then implement getStates like this:

Code: Select all

myController * inputClass::getStates()
{
    return &controller;
}
Keep in mind that the pointer that is returned by this implementation of getStates is only valid as long as the instance of inputClass still exists.
177
User avatar
rolland
Chaos Rift Regular
Chaos Rift Regular
Posts: 127
Joined: Fri Dec 21, 2007 2:27 pm
Current Project: Starting an Android app soon
Favorite Gaming Platforms: PS1, N64
Programming Language of Choice: C++
Location: Michigan, US

Re: segfault error; help please?

Post by rolland »

Thank you for pointing that out. After thinking about what you said, and some scroogling, I've implemented an initializer list. (doing so for a derived class of a similar set-up was a pain)

Code: Select all

inputClass::inputClass() : controller(new myController)
{
    SDL_Init(SDL_INIT_JOYSTICK);
    joystick = SDL_JoystickOpen(0);
    //std::cout << "joystick open" << std::endl;
    updateStates();
}
I'll write a signature once I get some creativity and inspiration...
Post Reply