views:

342

answers:

15

Hi,

I'm developing a game and when I do a specific action in the game, it crashes. So I went debugging and I saw my application crashed at simple C++ statements like if, return, ... Each time when I re-run, it crashes randomly at one of 3 lines and it never succeeds.

line 1:

if (dynamic) { ... } // dynamic is a bool member of my class

line 2:

return m_Fixture; // a line of the Box2D physical engine. m_Fixture is a pointer.

line 3:

return m_Density; // The body of a simple getter for an integer.

I get no errors from the app nor the OS...

Are there hints, tips or tricks to debug more efficient and get known what is going on?

That's why I love Java...

Thanks

+2  A: 

There are no simple C++ statements. An if is only as simple as the condition you evaluate. A return is only as simple as the expression you return.

You should use a debugger and/or post some of the crashing code. Can't be of much use with "my app crashed" as information.

DeadMG
+6  A: 

Random crashes like this are usually caused by stack corruption, since these are branching instructions and thus are sensitive to the condition of the stack. These are somewhat hard to track down, but you should run valgrind and examine the call stack on each crash to try and identify common functions that might be the root cause of the error.

Adam Shiemke
How to find the log-part of the crash?
Martijn Courteaux
@martijn: I'm not sure what you mean. You need to create a log file yourself, so it will be wherever you put it. A stack trace is usually generated whenever a 'crash' occurs. What debugger at you using?
Adam Shiemke
+3  A: 

Crashes / Seg faults usually happen when you access a memory location that it is not allowed to access, or you attempt to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location).

There are many memory analyzer tools, for example I use Valgrind which is really great in telling what the issue is (not only the line number, but also what's causing the crash).

Luca Matteis
+4  A: 

I generally like to take a second to step back and think through the code, trying to catch any logic errors.

You might try commenting out different parts of the code and seeing if it affects how the program is compiled.

Besides those two things you could try using a debugger like Visual Studio or Eclipse etc...

Lastly you could try to post your code and the error you are getting on a website with a community that knows programming and could help you work through the error (read: stackoverflow)

Jordan
+1, sometimes taking a step back and thinking through what your code is doing is the best way to find an error.
Silvae
Posting the code won't work, as some code far off the actual crashsite may modify some data it shouldn't modify and not make it into the question.
tstenner
A: 

Check pointers. At a guess, you're dereferencing a null pointer.

Paul Nathan
Not necessarily. Things can get fun even if pointer is no null.
SigTerm
@SigTerm: "At a guess"; that is, it's where I would look first. Of course things can get far uglier.
Paul Nathan
+4  A: 

These are mostly due to stack corruption, but heap corruption can also affect programs in this way.

stack corruption occurs most of the time because of "off by one errors".
heap corruption occurs because of new/delete not being handled carefully, like double delete or deleting a null pointer.

Basically what happens is that the overflow/corruption overwrites an important instruction, then much much later on, when you try to execute the instruction, it will crash.

Eric
A: 

I've found 'random' crashes when there are some reference to a deleted object. As the memory is not necessarily overwritten, in many cases you don't notice it and the program works correctly, and than crashes after the memory was updated and is not valid anymore.

JUST FOR DEBUGGING PURPOSES, try commenting out some suspicious 'deletes'. Then, if it doesn't crash anymore, there you are.

Diego Pereyra
+1  A: 

I had problems like this before. I was trying to refresh the GUI from different threads.

kist
+1  A: 

If the if statements involve dereferencing pointers, you're almost certainly corrupting the stack (this explains why an innocent return 0 would crash...)

This can happen, for instance, by going out of bounds in an array (you should be using std::vector!), trying to strcpy a char[]-based string missing the ending '\0' (you should be using std::string!), passing a bad size to memcpy (you should be using copy-constructors!), etc.

Try to figure out a way to reproduce it reliably, then place a watch on the corrupted pointer. Run through the code line-by-line until you find the very line that corrupts the pointer.

BlueRaja - Danny Pflughoeft
+1  A: 

Look at the disassembly. Almost any C/C++ debugger will be happy to show you the machine code and the registers where the program crashed. The registers include the Instruction Pointer (EIP or RIP on x86/x64) which is where the program was when it stopped. The other registers usually have memory addresses or data. If the memory address is 0 or a bad pointer, there is your problem.

Then you just have to work backward to find out how it got that way. Hardware breakpoints on memory changes are very helpful here.

On a Linux/BSD/Mac, using GDB's scripting features can help a lot here. You can script things so that after the breakpoint is hit 20 times it enables a hardware watch on the address of array element 17. Etc.

You can also write debugging into your program. Use the assert() function. Everywhere!

Use assert to check the arguments to every function. Use assert to check the state of every object before you exit the function. In a game, assert that the player is on the map, that the player has health between 0 and 100, assert everything that you can think of. For complicated objects write verify() or validate() functions into the object itself that checks everything about it and then call those from an assert().

Another way to write in debugging is to have the program use signal() in Linux or asm int 3 in Windows to break into the debugger from the program. Then you can write temporary code into the program to check if it is on iteration 1117321 of the main loop. That can be useful if the bug always happens at 1117322. The program will execute much faster this way than to use a debugger breakpoint.

Zan Lynx
+3  A: 
SigTerm
A: 

Refactoring.

Scan all the code, make it clearer if not clear at first read, try to understand what you wrote and immediately fix what seems incorrect.

You'll certainly discover the problem(s) this way and fix a lot of other problems too.

Klaim
+1  A: 

some tips :
- run your application under a debugger, with the symbol files (PDB) together.
- How to set Visual Studio as the default post-mortem debugger?
- set default debugger for WinDbg Just-in-time Debugging
- check memory allocations Overriding new and delete, and Overriding malloc and free

lsalamon
A: 

use the GNU Debugger

Silmaril89
+1  A: 

One other trick: turn off code optimization and see if the crash points make more sense. Optimization is allowed to float little bits of your code to surprising places; mapping that back to source code lines can be less than perfect.

jackr