[Solved]SegFault questions.

Get answers to all your basic programming questions. No Ogre questions, please!
Post Reply
big_o
Goblin
Posts: 279
Joined: Sun Feb 19, 2006 1:08 am

[Solved]SegFault questions.

Post by big_o »

I am finally getting around to debugging my game, and it will be the first c++ program I have debugged, and the first time I've dealt with segmentation faults.

I updated my project to Eihort and worked around some problems I had the last time I tried to do it, but now segmentation faults are popping up in what I thought was very mundane and safe code.

I guess what I'm asking is what generally causes code to segfault. I can backtrace and comment out the offending lines, but they occur in a few critical places.
Last edited by big_o on Thu May 31, 2007 3:43 am, edited 1 time in total.
User avatar
Kujara
Halfling
Posts: 73
Joined: Wed Nov 15, 2006 4:18 pm
Location: Paris, France

Post by Kujara »

Segfault are caused by bad access to memory, in almost all cases :

Trying to delete a pointer 2 times in a row.

Trying to use a pointer that doesn't point on anything, or points to something wrong.

Bad casts then access to a function/member variable that doesn't, in fact, exist.

Losing track of a pointer, then the garbage collector will try to delete it at the end of the program, possibly causing an error if the destructor relied on something else existing.

Exceeding the bounds of an array, which leads to you using something you're not supposed to touch.
big_o
Goblin
Posts: 279
Joined: Sun Feb 19, 2006 1:08 am

Post by big_o »

Thanks.

The 1st, 2nd, and 4th are possible.

Time to experiment :) .
User avatar
Emmeran
Goblin
Posts: 272
Joined: Wed Jun 02, 2004 11:47 am
Location: Erlangen
Contact:

Post by Emmeran »

the most common cause is a corruption of the heap.
for detailed informations with examples you can look here too:
http://www.ogre3d.org/wiki/index.php/Heap_Corruption
=PENTACHRON=
A real time strategy game using OGRE.
http://www.pentachron.net
User avatar
SpaceDude
Bronze Sponsor
Bronze Sponsor
Posts: 822
Joined: Thu Feb 02, 2006 1:49 pm
Location: Nottingham, UK
x 3
Contact:

Post by SpaceDude »

One more thing, if you are using several external libraries. You can often get segmentation faults if you compile your code against a new version of the library without recompiling the .dll or .lib files of that library. If your own code looks sound, just try recompiling and linking everything.
big_o
Goblin
Posts: 279
Joined: Sun Feb 19, 2006 1:08 am

Post by big_o »

I got it working. It was a quirk in the game state code from the wiki.

In the exit method for the states I do a lot of cleanup code, and the whenever states are changed the framestarted method for the previous state is called one last time, after its exit method has already been called.

At least that is what it looks like its doing, and the only thing that can explain my problem.

When the return to main menu button is clicked in my game the game is paused, but when the exit method is called it is unpaused, and then the program attempts to update the player one last time, which is impossible since the player no longer exists.

Thanks for the help.
Post Reply