Guard blocks and Red Zones complement each other in several ways and can be used together to find your memory corruption problem. While Red Zones have a high memory consumption overhead, they provide immediate notification when an illegal read or write occurs. Guard blocks add minimal overhead, but detect only illegal writes, and report corruptions only when the block is deallocated or when you generate a report. Finding a memory problem with MemoryScape may require a couple of passes to narrow down your problem.
Start by enabling guard blocks prior to running your program. You can run a Corrupt Memory report at any time to see whether you have any corrupted blocks. The report shows the blocks that are corrupt and where they were initially allocated. In your next run, turn off guard blocks and turn Red Zones on. If memory is tight, enable Red Zones only where needed, either manually or by using size ranges. MemoryScape should detect when a block is overwritten and stop execution.
A caveat here: the layout of memory is controlled by the heap manager and the operating system. Depending on the size of your allocated block and its alignment on the page of memory, there may be a gap between the block and the Red Zone. The overrun or underrun must be large enough to span the gap and reach the Red Zone, or MemoryScape will not generate an event. This is a potential issue only if you are using
memalign or
posix_memalign. For
malloc and
calloc, the gap will probably be one less than the size of the alignment, three or seven bytes. In any case, the block will still show up as having a corrupted guard block, because guard blocks are placed immediately before and after your allocated block without any gap.