COM provides an interface for developers to use to debug their memory allocations: the IMallocSpy interface. For each method in IMalloc, there are two methods in IMallocSpy, a "pre" method and a "post" method. Once a developer implements it and publishes it to the system, the system calls the IMallocSpy "pre" method just before the corresponding IMalloc method, effectively allowing the debug code to "spy" on the allocation operation, and calling the "post" method to release the spy.
For example, when COM detects that the next call is a call to IMalloc::Alloc, it would call IMallocSpy::PreAlloc, executing whatever debug operations the developer wants during the Alloc execution, and then, when the Alloc call returns, call IMallocSpy::PostAlloc to release the spy and return control to the code.