André Puel
André Puel

Reputation: 9179

How to Detect Wrong Write

In my code, I have a object whose contents is fully garbage. I guess in other part of code a wrong write happened and by (un)luck it wrote at the address of object I mentioned.

I was wondering if there is some tool that can tell me every write that a memory address receives, so I can track the origin of this bug.

Upvotes: 1

Views: 133

Answers (3)

Michael
Michael

Reputation: 66

If you're on Windows, you can use Global Flags (gflags.exe, part of Debugging Tools For Windows) and turn PageHeap on. (On the Image tab, type the name of your .exe, then when it shows up click on the the "Enable Page Heap" checkbox.) Then run your program.

Just remember to turn it off again when you're done.

Upvotes: 0

Eugene
Eugene

Reputation: 7258

Most debuggers support breakpoints on write.

For example in visual studio you have to launch your app in debugger, break in (on a normal breakpoint close to initialization of suspect variable), then go "Debug/New Breakpoint/New Data Breakpoint" in the menu.

In SoftIce you can use BPM command. :) That one can also break on memory access.

Upvotes: 3

SigTerm
SigTerm

Reputation: 26409

Depending on your platform, you should be able to lock that region of memory using something like VirtualProtect (I think it is mprotect on linux). This way you'll get accessviolation/segfault when memory region is accessed improperly. And when you get accessviolation/segfault, you can catch them in debugger.

However, to lock memory region, normally region should be aligned to memory page (at least on windows), which could be a problem.

Aside from that you could use data breakpoints in your debugger.

Upvotes: 0

Related Questions