Delphi automatically manages memory
Atomic variables in Delphi, such as Integer, Boolean, Record, Enumeration, etc., are automatically allocated by the compiler for memory within the scope and automatically released when out of the scope. In addition, strings, Variants, dynamic arrays, and interfaces are also automatically managed by Delphi.
These variables are all stored on the stack , except for interfaces. In addition, Variant is manually created by programmers using functions, such as VarArrayCreate; dynamic arrays are also manually created by programmers using functions, such as SetLength, but in both cases, programmers do not need to release it themselves.
In addition, there is another kind of variable that needs to be noted, which is a variable declared using Threadvar, and its scope is a thread. This is mainly used when writing threaded functions so that each thread uses one thread-local storage.
Memory manually managed by programmers
Pointers and objects require programmers to manually apply for and release memory.
Pointers include PChar, Pointer (untyped pointer), record pointer, variable pointer (pointing to atomic variable), function pointer (such as callback function, divided into global functions and object methods). Use the New function to apply for memory, and use Dispose to release the pointer. In addition, GetMem, ReallocMem and FreeMem are also a series of functions for applying for and releasing memory. You can read and set Delphi's three memory management functions through the GetMemoryManager and SetMemoryManager functions.
Objects include subclass objects inherited from two inheritance trees, TObject and IUnknown. Objects must be constructed using constructor methods. If you do not specify an owner for an object created using the constructor method (usually Create, but it may not be the case; Delphi's compiler only recognizes the constructor keyword), you must manually release it yourself. Even if you specify an owner, you still need to check the actual situation. It needs to be released at a specific moment. Generally, the Free method is used for release (IUnknown does not require manual release). A better method is to use FreeAndNil (in the Sysutils.pas unit), which not only releases the occupied memory but also releases the pointer itself.
These variables are stored in the heap . Another issue that needs attention is that the pointers in TList need to be released by the programmer himself.
Note:
1. The callback function does not belong to the technical scope of memory management. For details, please refer to the "Pointer" section.
2. For details about the object, please refer to the "Object Model" section.
3. If a pointer is used in multiple places, and if one of the places is released, an exception will occur if it is used in other places. This is a problem that should be paid attention to when using pointers. For detailed memory information, please refer to the "Pointer" section.
4. Passing pointers has two obvious advantages: saving memory and improving speed. For detailed memory information, see the "Pointers" and "Design Tips" sections.
5. How to judge objects and pointers is a very technical topic. There are some posts on this topic on csdn. You can also see an article written by Aimingoo ([email protected]) titled "About "How to detect whether a pointer is Article "In-depth Discussion of Object". For detailed memory information, see the "Pointer" and "Object" sections.
Completed on 2004-1-19 .
All rights reserved. Please feel free to correct any inappropriate comments.