Look at the disassembly. Almost any C / C ++ debugger will gladly show you the machine code and registers in which the program crashed. Registers include an instruction pointer (EIP or RIP on x86 / x64) where the program was stopped. Other registers usually have memory addresses or data. If the memory address is 0 or a bad pointer, your problem arises.
Then you just need to work back to find out how it happened. Hardware breakpoints when changing memory are very useful here.
On Linux / BSD / Mac, using GDB scripting functions can help here. You can script things so that after the breakpoint has hit 20 times, it allows the hardware clock to address the array element 17. Etc.
You can also write debugging to your program. Use the assert () function. Everywhere!
Use assert to check the arguments for each function. Use assert to check the status of each object before exiting a function. In the game, confirm that the player is on the map, that the player has health from 0 to 100, they say everything you can think of. For complex objects, write the verify () or validate () functions in the object itself, which checks everything about it and then calls them from assert ().
Another way to write to debugging is to use the program signal () on Linux or asm int 3 on Windows to exit the debugger from the program. You can then write the temporary code to the program to check if it is in iteration 1117321 of the main loop. This can be useful if the error always occurs in 1117322. The program will run much faster than using the debugger breakpoint.
Zan lynx
source share