VS debugging and watching a variable for changes

2020-07-03 08:10发布

问题:

I have a property inside a class that is getting changed by something. The only place I change the value of this code is a line that looks like this:

pushpin.Position.Altitude = -31;

During visual studio debugging, is there a way to watch .Altitude for any changes made, preferably it breaks at the assignment statement that changes the value.

If this is the correct way to track down this problem, could I have a step-by-step tutorial/instruction on how to do this?

Thanks.

回答1:

If this is a property then you can do this by adding a breakpoint to the set method of the property. Putting the cursor in the set statement and hit F9 will create the break point.

If this is a field then there's no way to watch this directly. Breaking when a field changes a value is a supported operation in C++, known as data break points, but is not supported in the CLR. The best work around is to convert the field to a property temporarily and break on the set statement.

EDIT

Updating based on OP saying it's a 3rd party DLL.

In this case you want to use the Break at Function feature of Visual Studio. The first step is to disable Just My Code.

  • Tools -> Options -> Debugger
  • Uncheck "Enable Just My Code"

Next actually set the named break point

  • Open up the break points window (Debugger -> Windows -> Break Points)
  • Click on the new button and select "Break at function"
  • Enter the name of the property. For example: Position.set_Altitude

You may need to fully qualify the name in order to get it to work



回答2:

You can set a conditional breakpoint by setting the bp and then right-click to specify a condition upon which to break at that line.

You can add a 'Watch' to a variable and specify to break anywhere/anytime the value changes.



回答3:

You need to set a data breakpoint. A data breakpoint will cause the debugger to break when a certain memory address has changed.

More detailed description and instructions how to set a data breakpoint.