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). Visual Studio can be configured to launch FlexSim and automatically attach to it when you want to debug the DLL. To set this up, follow the steps below:
- From the Project menu, select [ProjectName] Properties.
- Ensure that the current platform and build settings are correct.
- In the tree menu on the left, expand the Configuration Properties option and select Debugging.
- In the Command field, enter the complete path to FlexSim (i.e. C:\Path\To\flexsim.exe).
- Set the Attach option to No.
Now, when you begin debugging, Visual Studio will launch FlexSim and attach to it automatically. FlexSim may take longer to load when Visual Studio is attached.
You can also attach Visual Studio to FlexSim manually. From the Debug menu, select Attach to Process..., select flexsim.exe from the list of processes, and click Attach.
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.