The debugger integrated into the IDE (Integrated Development Environment) of visual studio is very powerful. It allows you to track down hard to find bugs.
To put a break point in your code press F9. A red circle will appear next to the line. When you run the application it will stop at the line and enter the debugger. At this point you can examine variables by right clicking on them and selecting quick watch or typing them into the watch window in the bottom right of the screen. If the watch window is not available enable it from the debug menu. For watching DirectX interfaces look at this answer: here.
To step into the next line of code press F10.
If the line you are on is a function call F10 will skip the function, if you want to step into a function press F11.
To continue running your application press F5.
If an assert fails in your code a box will appear with abort, retry and ignore options. Pressing retry will enter the debugger where you can look to see what the problem is. If an unhandled exception occurs just pressing OK will drop you into the debugger.
There is a good chance the application will be in a piece of assembler code (some external library code for which you do not have the source), you want to find out where the problem is so open the call stack view (via the menu option view/debug windows/call stack) if it is not already open. This shows all the function calls leading up to the point the application is currently stopped at. Double click on any of these to go to that pieces of code where you can examine variables etc. as described above.
Another nice way of helping find bugs is to output results to the output window in Viz. You do this using the OutputDebugString command. This takes a formatted character string.
If you want to output variables you will need to create the character string in advance:
sprintf(buf,”The value of variable num is %d \n”,num);
In C++ you could use the string type. Since the output command requires a char pointer to use this function with a C++ string you would need to do this:
Outputting values like this is a very good way of debugging but you do not always want to declare a buffer and use sprintf better to create a function that does it on a passed string. To do this we need to use the variable parameters capability of C, we can declare a function that will take a number of variable parameters like this:
void MyOutputFunction(const char *str, …)
The three dots represent the variable parameters. To parse it we use the va_ (variable) set of functions:
We can then use the above function with variable parameters just like printf e.g.
MyOutputFunction(“The value of a is %d”,a);
MyOutputFunction(“The float value is %f the string is %s\n”,aFloatValue,aCharString);
A C++ method of the above avoiding char can be found via the String Handling notes.
Advanced: if you have a lot of debug information to output you should consider outputting it to a file. A nice way is to output it as HTML It is fairly easy to write basic HTML and it allows you to make your debug file much easier to read.