Search code examples
c++methodssegmentation-faultcall

Segfault when calling a method c++


I am fairly new to c++ and I am a bit stumped by this problem. I am trying to assign a variable from a call to a method in another class but it always segfaults. My code compiles with no warnings and I have checked that all variables are correct in gdb but the function call itself seems to cause a segfault. The code I am using is roughly like the following:

class History{
 public:
 bool test_history();
};
bool History::test_history(){
    std::cout<<"test"; //this line never gets executed
    //more code goes in here
    return true;
}


class Game{
 private:
    bool some_function();
 public:
    History game_actions_history;

};


bool Game::some_function(){

  return game_actions_history.test_history();

}

Any tips or advice is greatly appreciated!

EDIT: I edited the code so there is no more local_variable and the value returns directly. But it still segfaults. As for posting the actual code, it's fairly large, what parts should I post?


Solution

  • From what I can see there's nothing wrong with the code you've displayed. However, segfaults often are a good indication that you've got corrupted memory. It's happening some place else besides what you've shown and only happens to impact the code here. I'd look any place you're dealing with arrays, pointers, or any manual memory interactions.