Debugging C++ code

Runtime crash

Runtime crash means that your program may compile and execute, but suddenly stops after a few seconds.
Here are a few steps to follow in order to help you find the origin of the bug.

1. Look at the command line

The very first step to do before anything else is to look at the messages written on the command line.
Most of the time, the incorrect use of an element of the CGP library will be associated with an error message indicating its cause (ex. index exceeding the size of a buffer, external file not found), and the library may even indicate some hints to solve it.
assets/debug_visual.jpg
With Visual Studio/Windows, you have to check explicitly the command line, possibly hidden beside other windows.
assets/debug_qtcreator.jpg
With QtCreator, the output is visible in the "Application Output" tab on the bottom part.

2. Output values

The simplest way to check the value of some of the variables is to display/print their content on the command line using
std::cout<< yourVariable <<std::endl;
This approach is simple and fast when you have a good idea of the variable to look upon.
It reaches, however, quickly its limit when the erroneous value is in a long loop, or during the animation loop (too many values will be displayed on screen). The use of call stack + breakpoints can be a more powerful approach.
Note:

3. Call Stack trace

It may sometimes be hard to know where the error exactly takes place
When the program stops using abort() (or at a segmentation fault), the use of the "Call stack backtrace" displays the successive hierarchy of calling functions from the final abort() up to the main call. This allows to precisely identify from where the error is emitted (variable, file and line numbers).
assets/visual_stack_trace.jpg
Visual Studio: Interactive navigation in the Call Stack to find the origin of the error.
assets/qtcreator_stack_trace.jpg
QtCreator: Interactive navigation in the Call Stack to find the origin of the error.
Type the following commands
# call gdb to your executable (adapt its name to your case)
gdb build/executableName

# Then in GDB interface:
# run the program until it crashes
run 

# display the stack of calling functions at the crash position
backtrace
assets/command_line_stack_trace.jpg
The call stack can be displayed in command line using gdb in Linux/Mac.

4. Break-point

Visual Studio and QtCreator come with a visual debugger enabling you to set break-points in the code.
Once run, the program stops at the breakpoint and you can inspect every variable from this point as well as advancing in the program either line-by-line; to enter/leave a function; or to advance up to the next breakpoint.
assets/visual_breakpoint.jpg
Breakpoint in Visual Studio.
assets/qtcreator_breakpoint.jpg
Breakpoint in QtCreator.
Note: Some variable may be hidden when the code is compiled with heavy optimizations. If you need to see them, you need to re-compile the code without the optimization options (debug in Visual Studio, -O0 with gcc or clang).