Sign in to follow this  
ill

Segmentation Fault when using getter and setter functions...

Recommended Posts

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...

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this