Break when a value changes using the Visual Studio debugger

Visual StudioDebuggingBreakpointsConditional Breakpoint

Visual Studio Problem Overview


Is there a way to place a watch on variable and only have Visual Studio break when that value changes?

It would make it so much easier to find tricky state issues.

Can this be done?

Breakpoint conditions still need a breakpoint set, and I'd rather set a watch and let Visual Studio set the breakpoints at state changes.

Visual Studio Solutions


Solution 1 - Visual Studio

In the Visual Studio 2005 menu:

Debug -> New Breakpoint -> New Data Breakpoint

Enter:

&myVariable

Solution 2 - Visual Studio

You can also choose to break explicitly in code:

// Assuming C#
if (condition)
{
    System.Diagnostics.Debugger.Break();
}

From MSDN:

> Debugger.Break: > If no debugger is attached, users are > asked if they want to attach a > debugger. If yes, the debugger is > started. If a debugger is attached, > the debugger is signaled with a user > breakpoint event, and the debugger > suspends execution of the process just > as if a debugger breakpoint had been > hit.

This is only a fallback, though. Setting a conditional breakpoint in Visual Studio, as described in other comments, is a better choice.

Solution 3 - Visual Studio

In Visual Studio 2015, you can place a breakpoint on the set accessor of an Auto-Implemented Property and the debugger will break when the property is updated

public bool IsUpdated
{
    get;
    set;    //set breakpoint on this line
}

Update

Alternatively; @AbdulRaufMujahid has pointed out in the comments that if the auto implemented property is on a single line, you can position your cursor at the get; or set; and hit F9 and a breakpoint will be placed accordingly. Nice!

public bool IsUpdated { get; set; }

Solution 4 - Visual Studio

Imagine you have a class called A with the following declaration.

class A  
{  
    public:  
        A();

    private:
        int m_value;
};

You want the program to stop when someone modifies the value of "m_value".

Go to the class definition and put a breakpoint in the constructor of A.

A::A()
{
    ... // set breakpoint here
}

Once we stopped the program:

Debug -> New Breakpoint -> New Data Breakpoint ...

Address: &(this->m_value)
Byte Count: 4 (Because int has 4 bytes)

Now, we can resume the program. The debugger will stop when the value is changed.

You can do the same with inherited classes or compound classes.

class B
{
   private:
       A m_a;
};

Address: &(this->m_a.m_value)

If you don't know the number of bytes of the variable you want to inspect, you can use the sizeof operator.

For example:

// to know the size of the word processor,  
// if you want to inspect a pointer.
int wordTam = sizeof (void* ); 

If you look at the "Call stack" you can see the function that changed the value of the variable.

Solution 5 - Visual Studio

Change the variable into a property and add a breakpoint in the set method. Example:

private bool m_Var = false;
protected bool var
{
    get { 
        return m_var;
    }

    set { 
        m_var = value;
    }
}

Solution 6 - Visual Studio

If you are using WPF, there is an awesome tool : WPF Inspector.
It attaches itself to a WPF app and display the full tree of controls with the all properties, an it allows you (amongst other things) to break on any property change.

But sadly I didn't find any tool that would allow you to do the same with ANY property or variable.

Solution 7 - Visual Studio

Update in 2019:

This is now officially supported in Visual Studio 2019 Preview 2 for .Net Core 3.0 or higher. Of course, you may have to put some thoughts in potential risks of using a Preview version of IDE. I imagine in the near future this will be included in the official Visual Studio.

https://blogs.msdn.microsoft.com/visualstudio/2019/02/12/break-when-value-changes-data-breakpoints-for-net-core-in-visual-studio-2019/

> Fortunately, data breakpoints are no longer a C++ exclusive because they are now available for .NET Core (3.0 or higher) in Visual Studio 2019 Preview 2!

Solution 8 - Visual Studio

I remember the way you described it using Visual Basic 6.0. In Visual Studio, the only way I have found so far is by specifying a breakpoint condition.

Solution 9 - Visual Studio

Right click on the breakpoint works fine for me (though mostly I am using it for conditional breakpoints on specific variable values. Even breaking on expressions involving a thread name works which is very useful if you're trying to spot threading issues).

Solution 10 - Visual Studio

As Peter Mortensen wrote:

> In the Visual Studio 2005 menu: > > Debug -> New Breakpoint -> New Data Breakpoint > > Enter: &myVariable

Additional information:

Obviously, the system must know which address in memory to watch. So

  • set a normal breakpoint to the initialisation of myVariable (or myClass.m_Variable)
  • run the system and wait till it stops at that breakpoint.
  • Now the Menu entry is enabled, and you can watch the variable by entering &myVariable, or the instance by entering &myClass.m_Variable. Now the addresses are well defined.

Sorry when I did things wrong by explaining an already given solution. But I could not add a comment, and there has been some comments regarding this.

Solution 11 - Visual Studio

You can use a memory watchpoint in unmanaged code. Not sure if these are available in managed code though.

Solution 12 - Visual Studio

You can probably make a clever use of the DebugBreak() function.

Solution 13 - Visual Studio

You can optionally overload the = operator for the variable and can put the breakpoint inside the overloaded function on specific condition.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFlySwatView Question on Stackoverflow
Solution 1 - Visual StudioAShellyView Answer on Stackoverflow
Solution 2 - Visual StudioMichael PetrottaView Answer on Stackoverflow
Solution 3 - Visual StudioCraigView Answer on Stackoverflow
Solution 4 - Visual StudiomombocoView Answer on Stackoverflow
Solution 5 - Visual StudioMarcelloView Answer on Stackoverflow
Solution 6 - Visual StudioJulien NView Answer on Stackoverflow
Solution 7 - Visual StudioMattView Answer on Stackoverflow
Solution 8 - Visual StudioGulzar NazimView Answer on Stackoverflow
Solution 9 - Visual StudioOskarView Answer on Stackoverflow
Solution 10 - Visual StudioR RisackView Answer on Stackoverflow
Solution 11 - Visual Studio1800 INFORMATIONView Answer on Stackoverflow
Solution 12 - Visual StudiowipView Answer on Stackoverflow
Solution 13 - Visual StudioPRIMEView Answer on Stackoverflow