15

How I can set breakpoint on variable change (I think this is write access) in Visual Studio?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user694655
  • 287
  • 4
  • 11
  • 3
    possible duplicate of [Visual Studio Debugger + Break when a value changes?](http://stackoverflow.com/questions/160045/visual-studio-debugger-break-when-a-value-changes) – Matt Ball Jun 03 '11 at 07:07

5 Answers5

24

This is referred to as a Data Breakpoint in Visual Studio. To create one you'll need the address of the variable in question (just add &variableName) to the watch or immediate window. Then do the following

  1. Debug -> New Breakpoint -> New Data Breakpoint
  2. Enter the address in and size of the value in bytes

Note: This is only supported for C++ applications. Managed languages don't support data break points.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
3

You need to add "Has Changed" condition to your breakpoint. To do this:

  1. Set breakpoint on the line you want it to break when your variable is changed.
  2. Right-click red dot icon, select "Condition".
  3. Enter your variable name and select "Has Changed" option.

You may find more information in this MSDN how-to.

Yuri Stuken
  • 12,820
  • 1
  • 26
  • 23
  • Won't this just break on the line where the breakpoint is specified - instead of every time the given variable is written? – sdaau May 07 '20 at 00:34
2

This is now supported in VS2019 for . NET Core 3.0 or higher check this out

How do I set a data breakpoint?

Setting a data breakpoint is as easy as right-clicking on the property you’re interested in watching inside the watch, autos, or locals window and selecting “Break when value changes” in the context menu. All data breakpoints are displayed in the Breakpoints window. They are also represented by the standard, red breakpoint circle next to the specified property.

MoishyS
  • 2,334
  • 1
  • 6
  • 8
  • Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. – Armali Apr 03 '19 at 07:30
0

If you right click on the break point you can can set Conditions... This lets you specify a if a variable value is true or if its changed.
Break point conditions

Richard Forrest
  • 3,567
  • 2
  • 23
  • 32
  • Won't this just break on the line where the breakpoint is specified - instead of every time the given variable is written? – sdaau May 07 '20 at 00:34
-1

You can add a conditional breakpoint by:

  1. Add a normal breakpoint
  2. Right-Click on it and select "Condition"
  3. Select "Has changed"

The breakpoint will only be hit when the condition inside the textbox has changed.

As far as I'm aware, the condition inside the textbox needs to be written in the language you are debugging. I.e. in C#: x >= 5

If you are just looking for the change of a variable, you can simply add the variable itself to the TextBox and the breakpoint will be hit when the variable changes.

HTH, Christian

Christian
  • 4,345
  • 5
  • 42
  • 71
  • Won't this just break on the line where the breakpoint is specified - instead of every time the given variable is written? – sdaau May 07 '20 at 00:34