Name
FreeMem Procedure
Syntax
procedure FreeMem(P: Pointer);
Description
FreeMem frees the memory that P
points to. FreeMem uses the installed memory
manager to free the memory. FreeMem is not a real
procedure.
Tips and Tricks
Pcan benil, in which case,FreeMemreturns immediately.When freeing memory, always take care not to free a pointer more than once, and be sure not to refer to memory after freeing it.
To help avoid mistakes, especially in a multithreaded program, be sure to set global variables or class fields to
nilbefore freeing the memory. You want to avoid any windows—no matter how small—where a variable or field contains an invalid pointer. For example:
procedure FreeMemAndNil(var P);
var
Tmp: Pointer;
begin
Tmp := Pointer(P);
Pointer(P) := nil;
FreeMem(Tmp);
end;FreeMemin Delphi’s default memory manager is thread-safe, that is, you can callFreeMemfrom multiple threads simultaneously, but only ifIsMultiThreadis True.Call
FreeMemto free memory allocated byGetMem. CallDisposeto free memory allocated byNew.
See Also
| Dispose Procedure, IsMultiThread Variable, GetMem Procedure, ReallocMem Procedure, SetMemoryManager Procedure, SysFreeMem Function |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access