views:

57

answers:

2

I'm chasing a bug where a member value of an object seems to magically change, without any methods being called which modify it. No doubt something obvious but proving hard to track down. I know I can put conditional break-points in methods based on the variable value, but is it in any way possible to actually put a breakpoint on a variable itself? e.g a breakpoint which fires when x==4? I know I can put watches on, what about breakpoints?

Edit: this is a native-only project, no managed malarkey.

+4  A: 

You can use a data breakpoint. There are a number of restrictions about how and when they can be used, namely that they work only in native code.

(To the best of my knowledge, you can only tell it to break when the variable changes, not when it changes to a specific value, but I'm not entirely sure; most of my code is mixed managed/native and thus can't use data breakpoints).

James McNellis
+1 -- If this is as useful as it looks, it will help me solve a bug I've been chasing for the last three months.
Michael Myers
It didn't -- apparently it's not as helpful with arrays -- but I found the bug anyway, so I'm happy.
Michael Myers
It can even be done programmatically: http://www.morearty.com/code/breakpoint/
RaphaelSP
A: 

What you should do is just wrap the variable in a set/get - not just a template functions but actually in a separate class, where set/get MUST be used to access. Then put a breakpoint in there. Alternatively, for easier chop and change, you could wrap the value in a class, and use operator overloads (with appropriate breaks in) to alter. That's probably the cleanest and most portable solution.

What you may also find is that the variable being modified is not in ways you expect. Best example I've got is that I had unsigned int where I subtracted from zero when I meant to increment from zero, so when I was looking for places that I knew modified it, that didn't flag up. Couldn't work out wtf was going on.

However, as far as I know, VC++ supports no mechanism to break on arbitrary changes, if the data breakpoint won't work for you. for example, if it was changed due to stack/heap corruption. But if you're running in debug, I'd expect that VC++ would break on those.

DeadMG
So you want me to wrap every single variable in a special class, totally re-engineer the entire code base? But you still fail to realise values can change if you think you are looking at an object and you get your pointers mixed up for example, or try to reference an object after deleting it, etc.
John