开发者

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 i开发者_开发百科s 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?


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.


I have used valgrind succesfully with a lot of segfaults.

and have you tried to run gdb with the coredump caused by the segfault? from man gdb:

gdb program core

To create a coredump you might have to set:

ulimit -c unlimited


Shot in the dark. (Game*)this is NULL ?


The code is fine but the example is too incomplete to say what's wrong. Some things I'd suggest:

Add printouts to each class's destructor and constructor:

Game::Game()               { cerr << this << " Game::Game" << endl; }
Game::Game(Game const&)    { cerr << this << " Game::Game(Game const&)" << endl; }
Game::~Game()              { cerr << this << " Game::~Game" << endl; }
bool Game::some_function() { cerr << this << " Game::some_function()" << endl; ... }

This will reveal:

  • Null object pointers.
  • Bad/deleted class pointers.

Second, for debugging, I'd strongly recommended sending printouts to cerr instead of cout. cout is usually buffered (for efficiency) before being output, cerr is not (at least, this used to be the case). If your program quits without executing its error handlers, at_exit, etc..., you are more likely to see the output if it is unbuffered and printed immediately.

Thirdly, if your class declarations live in a header, the class definitions, live in one cpp file and the code that uses the class in yet another, you may get this kind of crash if either of the cpp files were not recompiled after you changed the header.

Some other possibilities are:

  • stack overflow: you've allocated a lot of memory on the stack because of deep recursion or are allocating objects containing large arrays of data as local variables (i.e. not created or the heap with new or malloc))
  • corrupted class vtable (usually only possible due to dependency errors in your build tools),
  • corrupted object vtable pointer: possible through misuse of pointers: using pointers to deleted memory, or incorrectly writing to an in-use address. Not likely in your example because there are no virtual functions.
  • maintaining a pointer or reference to an object allocated on the stack that has been deleted: the printout code above will uncover this case.


I am wondering because you have defined some_function() in private of the Game class. So the code structure which you have mentioned above will also throw error for that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜