Enabling Guard Malloc

Guard Malloc is a special version of the malloc library that replaces the standard library during debugging.

Guard Malloc uses several techniques to try and crash your application at the specific point where a memory error occurs.

For example, it places separate memory allocations on different virtual memory pages and then deletes the entire page when the memory is freed. Subsequent attempts to access the deallocated memory cause an immediate memory exception rather than a blind access into memory that might now hold other data. When the crash occurs, you can then go and inspect the point of failure in the debugger to identify the problem.


To enable debugging using Guard Malloc, configure your project to run with Guard Malloc in Xcode’s scheme editor.
Unfortunately, Enable Guard Malloc can be used only with the simulator.It cannot be used to debug on your device.
Enable Guard Malloc is a similar tool to Enable Zombie Enable, but it tracks problems with newand deleteor with mallocand  free. This can be used to track down memory violations in your C++ classes.

Enable Guard Malloc puts memory guards around memory every time it is allocated or freed. The net effect of this is that it can detect when something tries to use memory that has been freed/deleted. This is very useful for tracking down callback bugs.

// Test case for Enable Guard Malloc - delete and use
mTestCpp = new TestCPPClass();
delete mTestCpp;
mTestCpp->DoSomething();
}

Again, this is clearly a simple example; an object is being used after it has been deleted. However, this kind of thing can crop up in code quite easily without it being so obvious.  This can be especially problematic if two threads act on the same object in a nonthread-safe way. This is also quite easy to do in complex callback mechanisms.

Note:Enable Guard Malloc won’t find all of your memory stomps. It will find instances only where memory that has been marked as freed is changed.