Segmentation Fault when using getter and setter functions...

Started by
1 comment, last by ill 14 years, 5 months ago
So I have a very strange problem. I have a pointer to a class like... GameEngine * gameEngine = new GameEngine; It all works fine. But whenever I try to do gameEngine -> getSomeValue(); or gameEngine -> setSomeValue(); It results in a segfault when it accesses the variable it needs for reading or writing. It doesn't happen if I comment out the line where it either does the actual setting or getting. Like if I make it return NULL or not set anything at all, no segfault. Also I can call the function from the class itself and there are no problems, which normally is pointless since the class has access to its private variables, but just as a test, it ended up working... I have absolutely no idea why this could cause a segfault or how to fix it. Also the gameEngine pointer is a global variable, which I don't know if it has anything to do with it...
Advertisement
Have you tried using a debugger?

For example, if you have a more than one pointer called "gameEngine", you may only have initialised one of them.
Ah thanks..

I debugged it with gdb and saw the pointer was NULL everywhere.

What I did is in main I created the game engine and the game engine's constructor would start all subsystems and the main loop but never return from the constructor. This is why the global pointer to the game engine never got set.

Now I create the instance and start everything from main and it works perfectly...

This topic is closed to new replies.

Advertisement