While writing a new memory manager is a hard job, installing it in Delphi—once it is completed—is very simple. The System unit implements functions GetMemoryManager
and SetMemoryManager
that help with that:
type TMemoryManagerEx = record {The basic (required) memory manager functionality} GetMem: function(Size: NativeInt): Pointer; FreeMem: function(P: Pointer): Integer; ReallocMem: function(P: Pointer; Size: NativeInt): Pointer; {Extended (optional) functionality.} AllocMem: function(Size: NativeInt): Pointer; RegisterExpectedMemoryLeak: function(P: Pointer): Boolean; UnregisterExpectedMemoryLeak: function(P: Pointer): Boolean; end; procedure GetMemoryManager(var MemMgrEx: TMemoryManagerEx); overload; procedure SetMemoryManager(const MemMgrEx: TMemoryManagerEx); overload;
A proper way to install a new memory manager is to call GetMemoryManager
and store the result in some global variable. Then, the code should populate...