• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

setting breakpoint condition for variable value change

I have a variable that is setting itself back to 0 at some point in my code.  I have not been able to find that point.  I tried setting a breakpoint based on a change in the variable's value, but could not get that to work.  I don't understand how to set the 'context' properly in the breakpoint dialog box.  I referred to help, but I still don't think that I am setting it up properly.  

I start a new breakpoint, go to the Data tab, and set the variable name, language, item count, and condition.  What must be specified in the Context?  I think this is the part that I am missing.

3 Solutions
goto data tab
put following string with condition in expression list

{main,testcondefprinter.cpp,testcondefprinter} c == 2

//Here main is name of function.
//testcondefprinter.cpp is name of file. -file where variable is declared
//testcondefprinte is name of executable.
//c == 2 is name of variable

as you know the variable you must know the scope of that variable I mean in which file it is declared etc.
The 'context' property is documented in the help index under "context operator in debug expressions".

However, I have tried the "Data" breakpoint facility (mainly in earlier versions of Visual Studio) and never been able to get anything useful from it.  First it makes the program run incredibly slow because it has to check the variable after executing each line of code.  (I also think it checks after every single machine instruction if it can't find the source code!)

Also I think there are/were bugs in the debugger that meant it gave spurious results (both bogus finds, and missed ones).  Also the slowness of running can affect the behaviour of the program (eg, order that Windows events are received) so the problem may disappear.  Even if it works it may be due to a bad pointer and you might have to repeat the whole process (several times) to find the root cause.

You might instead try searching for all places the variable is modified and setting breakpoints there.  Of course, this will not find problems such as bad pointer bugs, in which case you could try a product such as Purify or Bounds Checker (although in my experience they don't always work either).  If you don't have access to those (or they find nothing) also:

1. Add TRACE statements at crucial places in the code.  (You should have some idea of where the problem is?)

2. Add TRACE statements before and after recent code changes if the problem has just appeared.

3. Turn on Buffer Security Check (Properties\C/C++\Code Generation) if it is an auto (local) variable.  (This is off by default in debug builds.)

4. Try setting normal (code) breakpoints at various places with a Condition like "var==0".

Note: If it is a member variable it might be useful to check the value whenever you execute a member function.  It is easy to set a breakpoint on all member functions by entering the class name in the Function: box in the New Breakpoint dialog (Function page).  Visual Studio then displays a list of member functions - just click the "All" button to set breakpoints on all of them. (Don't forget to click the "Condition" button first and set the condition as mentioned above.)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now