Breaking on variable modification

Started by
2 comments, last by Jan Wassenberg 18 years, 6 months ago
Moderators, feel free to move this if it doesn't belong here. This is more of a compiler question than programming question. It is possible to use private data for every structure in your program, then use interface functions to modify the data. Then it's as simple as placing a break point in that interface function to see when it is modified. Is it possible to break on variable modification without going through all of this? I'm using C++ in Visual Studio.net version 7. I really appreciate any suggestions :)
Advertisement
Yes. (Support is inconsistent, but if you're using VS on a Windows PC, it should work.) Go to the breakpoints window, create a new breakpoint (using 'New'). Click on the 'Data' tab, and enter the variable that you want to watch. In my experience:

1) Watching anything larger than 4 bytes is unlikely to work.
2) Watching things by name might not work. Instead, get the address of the variable you want, and put "*(int*)(0x12345678)" (replacing the address) into the box.
3) Setting more than one or two of these at a time will either not work, or make your app INCREDIBLY slow.

Still, highly useful once you know about it.
That's great :)

It worked perfectly by setting "no condition" and "break always", even with the variable name. I couldn't ask for better help. You're my hero. Thanks AP!
Yeah, this is invaluable for tracking down memory corruption. The "1..2" limitation is due to x86, which provides for 4 hardware (code or data) breakpoints. If you want more than this, VC is forced to single-step your program, thus explaining the slowdown.
These breakpoints can watch 1, 2 or 4 bytes, which accounts for that observation as well (although MS could have gone to the trouble of splitting larger variables into several "watchpoints").
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3

This topic is closed to new replies.

Advertisement