Watchpoints - Minding your Variables

wxDev-C++

<  Previous   <           = Home =             > Next >

Watching variables

    One of the most interesting aspects of debugging is the possibility to display the value of your variables at a given time. This way you can be sure your variable (e.g. a counter index) has the value you would expect.

    wxDev-C++ is able to conveniently show you the contents of your classes, strings, structures/unions, arrays and other variables in the Debug Local Variables window:

Note that in the above window, iTesta has already had its value set to "5", but iTestb has not been set (the value will be random and depends on what data is at that pointer at that given time). 

Now keep this Local Variables window open and re-start the debugging.  You should be able to "step through" the code after the breakpoint (discussed in the last section) and watch the iTesta variable change from a random value to "5".

Adding Watchpoints

Watchpoints are like breakpoints that are triggered not by a particular line of code, but by the change in value of a particular variable in code.  For example, let's say that you've got a large program and think that your variable iCount is being changed unexpectedly.  You can set a regular breakpoint (at any line number), run the debugger to the breakpoint, and then add a watch to iCount.  The debugger will then always break whenever the value of iCount changes.

To set a watchpoint, add a normal breakpoint to your code and run the debugger. On the Debug window, click on the Watch tab. Then, click anywhere within the Watch tab space. An Add Watch  (shortcut : F4) menu should appear.

Once you select Add Watch, a dialog box should appear:

Watch variable

    Type the name of your variable in the dialog, and press OK. If you select a word in the current source file and press F4, it will add a watch of the selected text without asking for a variable name. A breakpoint can be generated when the value of the variable is written, read, or both.

    Once a watchpoint variable is set, it will continue to be monitored in future debugging runs. You can also modify the value of your watched variable at any time during debugging by right clicking on the variable name in the Debug Watch window and selecting Modify value. Note that you are changing this value outside of your source code. The next time you run the debug, the value you entered is lost and the program executes normally.

Important Notes:

  • When using pointers to structures or classes, if you want to display all the members of variable my_pointer then you need to watch *my_pointer (‘*’ is the value-operator). Watching only my_pointer would just display the address contained in my_pointer.
  • Sometimes the debugger may not know the type of a pointer, and cannot display all of the members of the pointed structure or class. You can bypass this problem by casting your watched variable. For example, if the debugger cannot show the contents of my_pointer of type MyPointer, you could try adding the watch variable: *(MyPointer *)my_pointer
  • If you are watching a wxString variable, you might find that when the string changes, the debugger stops inside one of wxWidgets' string functions and pressing F7 has no effect. The solution is to set an ordinary breakpoint on the next line and press F8 to continue to the new breakpoint.
    This happens because GDB is a general-purpose debugger and it doesn't know that you are not interested in the internal workings of wxWidgets. It is normally seen as undesirable behaviour but it is not a fault.