lundi 21 septembre 2015

If statement with a class pointer is causing a segfault

I've made a finite state machine class for a little C++ project I'm working on, but for some reason, the program segfaults as soon as it gets to a certain if-statement.

Here is the class itself:

class FiniteStateMachine
{
protected:
    void cleanup();

    State* state_;

public:
    FiniteStateMachine();
    FiniteStateMachine( State* const state );
    ~FiniteStateMachine();

    void changeState();
    void forceState( State* const state );
    char const* getStateName();

};

And here in the definition, the program segfaults as soon as it gets to the conditional statement:

void FiniteStateMachine::changeState()
{
    debugging("FiniteStateMachine::changestate() called");

    if( state_ ) // problem point
    {
        debugging("state_ is not nullptr");
        // state_.handle();
    }
    else
    {
        debugging("state_ is nullptr");
    }
}

debugging() is just a function used to display messages to stdout while in debug mode. The strangest thing to me is that if( state_ ) is used earlier in the program without a hitch.

I'm using gcc version 4.9.2, Linux Mint 17.2, and C++11

If you need any further details, here is the link to the github repository.

Aucun commentaire:

Enregistrer un commentaire