Discussion:
breakpoint when a variable has changed
(too old to reply)
Cathy
2008-10-09 07:06:37 UTC
Permalink
Hello
How can I set a breakpoint that stops the program when a variable has
changed its value?
Thanks
Cathy
Martin T.
2008-10-09 07:19:30 UTC
Permalink
Post by Cathy
Hello
How can I set a breakpoint that stops the program when a variable has
changed its value?
You can't.

But:
In the VS2005 debugger you can right-click on a breakpoint and add a
[Condition] or [Hit count], so you can stop at the assignment of a
variable if it has a certain value ...

br,
Martin
Cathy
2008-10-09 11:09:16 UTC
Permalink
Post by Martin T.
You can't.
Ok thanks. That is strange, it would be often useful for me.
Cathy
Nathan Mates
2008-10-09 17:28:52 UTC
Permalink
Post by Cathy
How can I set a breakpoint that stops the program when a variable has
changed its value?
You can set up to 4 hardware breakpoints that stop the program when
a particular memory address is written to. As such, they're not easy
to use if you're trying to breakpoint a variable on the stack, as the
stack's memory addresses are generally recycled from function to
function. To set a breakpoint, do the following:

1) Obtain the address you wish to breakpoint at. I generally do this
in DevStudio's watch window, adding an entry for "&g_SomeVariable".
You'll get an address like 0x12345678

2) In the breakpoints window (alt-F9), go to New -> New Data
Breakpoint. If you don't see the entry for "New Data Breakpoint",
then go to the main DevStudio Tools -> Options... -> Debugging, and
check [x] Enable Address-level Debugging. Once address-level debugging
is enabled, then go back to the breakpoints window.

Side note: with address-level debugging, DevStudio 2003/2005 has
the REALLY ANNOYING behavior for me of treating F10 (step over when
stopped) as a "go into the disassembly window and step one
instruction" instead. For me, it does that, even if I didn't have the
disassembly window open, and was staring at the source code. I have
to go turn off address-level debugging, and then DevStudio will get a
clue and *stay* in the source window and step one C/C++ line at a time
when I hit F10. Very frickin' annoying. Fix it, MS!

3) Anyhow, once you you've selected New Data Breakpoint, in the window
that appears, enter the address (e.g. 0x12345678) where specified, and
hit ok.

Side note - this can't be used to easily breakpoint memory that's
changed due to a file load or other times the OS writes to your
memory. I seem to remember that the OS has a different view of your
app's memory, so the address entered won't trip the breakpoints.

Nathan Mates

--
<*> Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein
Cathy
2008-10-10 10:36:41 UTC
Permalink
Wow thanks Nathan, that's exactly what I was looking for and your
answer is really complete!
Cathy

Loading...