Visual Studio allows to set breakpoints on memory location only 4 bytes length (on 32-bit Windows version). To catch memory access (read or write) you could use the following class:
struct protect_mem_t {
protect_mem_t( void* addr, size_t size ) : addr(addr), size(size), is_protected(FALSE) {
protect();
}
~protect_mem_t() { release(); }
BOOL protect() {
if ( !is_protected ) {
// To catch only read access you should change PAGE_NOACCESS to PAGE_READONLY
is_protected = VirtualProtect( addr, size, PAGE_NOACCESS, &old_protect );
}
return is_protected;
}
BOOL release() {
if ( is_protected )
is_protected = !VirtualProtect( addr, size, old_protect, &old_protect );
return !is_protected;
}
protected:
void* addr;
size_t size;
BOOL is_protected;
DWORD old_protect;
};
It changes access mode on selected memory pages. Page size is equal to 4096 bytes on 32-bit systems. Exception will be thrown on every access to protected memory. This class is limited in use to only large memory areas, but I hope it can be helpful.
It could be used in the following way:
// some_array should be aligned on PAGE_SIZE boundaries
protect_mem_t guard( &some_array, PAGE_SIZE );