How can I stop execution in the Visual Studio Debugger when a private member variable changes value?

淺唱寂寞╮ 提交于 2019-12-07 06:13:35

问题


Let's say my class has a private integer variable called count.

I've already hit a breakpoint in my code. Now before I press continue, I want to make it so the debugger will stop anytime count gets a new value assigned to it.

Besides promoting count to a field and setting a breakpoint on the set method of the field, is there any other way to do this?


回答1:


What you're looking for is not possible in managed code. In C++ this is known as data break point. It allows you to break whenever a block of memory is altered by the running program. But this is only available in pure native C++ code.

A short version of why this is not implemented is that it's much harder in managed code. Native code is nice and predictable. You create memory and it doesn't move around unless you create a new object (or explicitly copy memory).

Managed code is much more complex because it's a garbage collected language. The CLR commonly moves objects around in memory. Therefore simply watching a bit of memory is not good enough. It requires GC interaction.

This is just one of the issues with implementing managed break points.




回答2:


I assume you're trying to do this because you want to see where the change in value came from. You already stated the way I've always done it: create a property, and break on the set accessor (except that you must then always use that set accessor for this to work).

Basically, I'd say that since a private field is only storage you can't break on it because the private field isn't a breakable instruction.




回答3:


The only way I can think do do this, is to right click on the variable, and select "Find all references". Once it finds all the references, you can create a new breakpoint at each point in the code where the variable is assigned a value. This would probable work pretty well, unless you were passing the variable in by reference to another function and changing the value in there. In that case, you'd need some way of watching a specific point in memory to see when it changed. I'm not sure if such a tool exists in VS.




回答4:


Like ChrisW commented. You can set a 'Data Breakpoint' but only for native (non-managed) code. The garbage collector will move allocated memory blocks around when the garbage collector runs. Thus, data breakpoints are not possible for managed code.

Otherwise, no. You must encapsulate access to your item for which you want to 'break on modify'. Since its a private member already, I suggest following Kibbee's suggestion of setting breakpoints wherever its used.




回答5:


Besides promoting count to a field and setting a breakpoint on the set method of the field, is there any other way to do this?

Make it a property of a different class, create an instance of the class, and set a breakpoint on the property.

Instead of ...

test()
{
   int i = 3;
   ...etc...
   i = 4;
}

... have ...

class Int
{
  int m;
  internal Int(int i) { m = i; }
  internal val { set { m = value; } get { return m; } }
}

test()
{
  Int i = new Int(3);
   ...etc...
   i.val = 4;
}

The thing is that, using C#, the actual memory location of everything is being moved continually: and therefore the debugger can't easily use the CPU's 'break on memory access' debugging register, and it's easier for the debugger to, instead, implement a code-location breakpoint.



来源:https://stackoverflow.com/questions/517057/how-can-i-stop-execution-in-the-visual-studio-debugger-when-a-private-member-var

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!