Delphi programming skills (1)
Memory management
Delphi automatically manages memory
The atom variables in Delphi, such as Integer, Boolean, Record, enumeration, etc. are automatically applied for memory in the scope of the compiler, and the scope is automatically released; in addition, strings, variant, dynamic arrays, interfaces are also automatically managed by Delphi.
These variables are stored in the stack, except for the interface. In addition, Variant is manually created by programmers, such as VararrayCreate; dynamic arrays is also manually created by programmers, such as setlength, but these two situations do not need to release themselves.
In addition, there is also a variable that is to be aware that the variables declared using the Threadvar, and its scope is a thread. This is mainly used in writing a thread function, each thread uses a thread local storage.
Programmer manually managing memory
Pointers and objects are memory that requires programmers to apply for and release.
The pointer includes PCHAR, POINTER (no type pointer), record pointer, variable pointer (pointing atom variable), function pointer (such as a callback function, is 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 applications that release the memory function, which can read and set up the three memory management functions of Delphi via the GetMemoryManager and SetMemoryManager functions.
The object includes the subclass objects inherited by TOBJECT and IUNKNOWN inherited trees. Construction methods must be used to construct the object. The object created by the construction method (generally CREATE, or not, Delphi compiler only recognizes the keyword of constructor). If you don't specify the owner, then you must manually release it, even if you specify the owner, you need to see the actual Need to be released at a specific moment. Release the general use of the free method (IUnknown does not require manual release), a better way is to use Freeandnil (in the SYSUTILS.PAS unit) that releases the occupied memory while release the pointer itself.
These variables are stored in the heap. Another problem that needs attention is that the pointers in TLIST require programmers to release themselves.
Note:
1. The callback function does not belong to the technical scope of memory management, see "Pointer" section for details.
2. See the Object Model section for details.
3, a pointer is used in multiple places, and one of them is released, and the other places will have an exception, which is a problem that should be paid attention to using a pointer. See the "Pointer" section in detail.
4, there are two obvious advantages of passing the pointers: saving memory and improving speed. See "Pointer" and "Design Skills" section in detail.
5, how to determine that the object and pointer is a strong technical topic, there are some post on 9CBS, and you can see if "about" is "about" how to detect pointers. The article "in-depth discussion" of the object. See "Pointer" and "Object" section in detail.
Completed on 2004-1-19.
Copyright, please don't finish correct.