views:

780

answers:

6

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  A: 

Assertion

cmsjr
A: 

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.

JMD
A: 

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.

Kibbee
A: 

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.

Matt Brunell
A: 

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.

ChrisW
+2  A: 

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.

JaredPar