Debugging
Description
Visual Studio provides very powerful debugging tools. These tools can greatly reduce the amount of time required to identify and fix bugs in a project, and can greatly aid in DLL development.
Preparing to Debug
Before debugging, ensure that your DLL is built using a Debug configuration. To change configurations, select Debug from the Solution Configurations combobox.
Attaching the Visual Studio Debugger
In order to debug a DLL, Visual Studio must be attached to a program that uses the DLL (FlexSim, in this case). In order to attach Visual Studio to FlexSim, follow these steps:
- Launch FlexSim and wait for the main window to open.
- In Visual Studio's main menu, point to Debug and choose Attach to Process.... This opens the Attach to Process window.
- In the list of Available processes, you'll see multiple instances of flexsim.exe. Select the one that has a value in the Title column.
- Click the Attach button.
When Visual Studio is attached, the status bar at the bottom will turn from blue to orange.
Breakpoints
Setting breakpoints is very easy in Visual Studio; just click to the left any line of code. A red octagon will appear. When the program is about to execute that command, Visual Studio will pause the program. From here, you can perform the usual breakpoint tasks: viewing the values of local variables, stepping in, out, and over code, etc.
Conditional Breakpoints
Once a breakpoint is set, you can make it a conditional breakpoint by right-clicking the breakpoint and selecting Condition.... When the specified condition is true, the program execution will stop at the breakpoint. This is very useful for debugging code that executes often.
Printing Breakpoints
A breakpoint can also become a printing breakpoint by selecting the When Hit... right-click option. This kind of breakpoint simply prints a custom message (which can involve current variable values) to the output window, and (optionally) does not halt execution. This can replace hard-coded print commands.
The Watch Window
The watch window (available from the Debug->Windows menu) contains a list of user-defined expressions. These expressions can be anything as simple as a variable, or they can be more complex statements, including some function calls. The watch window provides a convenient way to watch a select group of variables.
The Autos Window
The autos window (available from the Debug->Windows menu), keeps a dynamic list values. These values not only include local variables, but also the values returned by function calls, even if those values are not assigned to a variable. This allows you to see all the values involved in a complex line of code.
Spy++
Spy++ is an application (available from the Tools menu) that can display messages sent to and from particular windows. To monitor a specific window, follow the steps below:
- Click the button.
- Drag the Finder Tool to the window of interest
- In the Show panel, select the Messages radio option.
All messages sent to and from the selected window, along with the values sent with the message, will be displayed in the Spy++ window.