Delphi.memory. Allocation and release---new/dispose, getmem/freemem and other functions differ from the same, memory allocation function

Source: Internet
Author: User

From: http://www.cnblogs.com/qiusl/p/4028437.html?utm_source=tuicool&utm_medium=referral

--------------------------------------------------------------------------------------------------------

I reckoned the memory allocation + release is a basic function, some people may not pay attention to such functions or scrutiny, but I think it is good to understand.

Introduction of the following memory functions before the first to say a few mm process, such as do not care can be ignored:

Record  function(size:integer): Pointer;   function (p:pointer): Integer;   function (P:pointer; Size:integer): Pointer; End ; var    = (    getmem:sysgetmem;    Freemem:sysfreemem;    REALLOCMEM:SYSREALLOCMEM);

The above is the D7 version of the MM function, where the variable Memorymanager I call the mm function , please note.

d2005-d2007 above version (not confirmed which version), mm function more AllocMem and registerleak/unregisterleak function, and this article has nothing to say.

The third party mm takes over is this mm several functions, achieves the plug-in goal, and the SYS starts the SYSGETMEM, Sysfreemem, Sysreallocmem is itself the system own mm processing.

One: New/dispose

This two functions, the estimation learns delphi/pascal, knows: Allocates and frees the memory block for Record/object this kind of data

Then the allocation and deallocation is called the GETMEM/FREEMEM function, which differs from the GETMEM/FREEMEM:

New () After Getmem, the Initialize (x) operation is performed, that is, the initialization of the Record/object data.

Initialize function, in the system unit, the function is plainly, that is, record/object inside, containing string,interface, dync Array,variant,record, Array of fields, initialized to 0 (empty).

This step is important because the memory blocks returned by Getmem may have been reused, used, and represented with values.

The value of the case, and then re-assignment, it means that the old address corresponding to the data should be emptied first, empty the random address of the data? The AV will appear ...

(Do not think, after the GETMEM, the initialization of each field, error-prone is this, in the case of the above with the field, if you need to manually initialize, you must use Fillchar, for reasons such as above.) )

The opposite of Dispose () is also, counter-operated, emptied: Finalize (x), then freemem to ensure Record/object,

The String,interface/dyncarray field is not compromised by calling Freemem directly (leak)

The summary is:

A:new==> Getmem (p, sizeof (Tdatatype)) + Initialize (p^) ==>allocmem (sizeof (Tdatatype));

It differs from the AllocMem: Initialize (x) does not clear 0 for each byte, only for some words Chingqing 0.

Dispose = = Finalize (p^) + FREEMEM (p);

There is no substitute function, and it is not necessary to finalize (p^) This step, otherwise there will be leak.

B:record/object the pointer type, it is best to use this to allocate and release the function. Of course you can also go to the self-maintenance record/object inside the field lifetime.

C: If system is called. Initialize/finalize, the prompt appears:

[Hint] Unit1.pas: Expression needs no initialize/finalize

Represents the field inside the Record/object and does not contain String,interface,dync Array,variant,record,array

This means that you do not need to call initialize or finalize for action.

D: Say one more thing: each warn/hint has its role, do not ignore, perhaps the small bug is in it, please pay attention to them or kill them.

Two: Getmem/freemem

Getmem/freemem is the allocation of MM and the release of memory block function, more say some is related to: This two functions, because of allocation or release failure to throw an exception (exception)

The standard allocation and release function of MM corresponds to the return value pattern, that is, fails, and returns only null (nil) or not 0, not an exception.

That is to say, Get/freemem is an exception encapsulation for the standard function of MM.

Exception information:

Getmem fail = out of memory.

Allocation failures typically occur only when the process's available memory is allocated, usually in the case of a memory leak.

Freemem fail = Invalid pointer operation

Two times Freemem with the address, the second time there is this invalid pointer abnormal. :)

Three: Getmemory/freememory

Get/freememory is basically the same as Getmem/freemem, except that it returns directly to the return value of the corresponding function of mm without exception handling.

That is: Getmem call Mm.getmem returns NIL, there is an exception, and GetMemory returns nil directly to the caller to handle

The FREEMEM call Mm.freemem returns a non-0(Error-free) exception, and Freememory returns 0 or not 0 directly to the caller for processing.

This is useful when writing programs that can reduce exceptions, or write an assert (...) when a get/free error occurs, letting the program break down, check and debug.

Four: Sysgetmem/sysfreemem

Sysgetmem/sysfreemem is basically the same as getmemory/freememory, except that it calls the implementation function of MM directly,

The manager pointer does not go through the mm and then jumps.

That is: Sysget/sysfreemem, it uses the system comes with the MM allocation release function, when the third party mm joined, the above three pairs of functions,

will be taken over by a third party mm, but sysget/sysfreemem it is also called the system's own mm function processing, and third-party mm independent.

V: Other

Some other Delphi unit allocation release function, but basically from the above four to extend the function, it is not clear

Of course there are allocation + release functions that extend from the API, which is not the case, regardless of the mm extension of the D system.

Summarize:

New+dispose and Getmem+freemem are allocation + release functions that are based on VCL exception mechanism protection.

Getmemory+freememory and Sysgetmem+sysfreemem are returned by the caller's own control to determine whether to return an exception or error handling.

Finish.

2014.10.19 by QSL

Delphi.memory. Allocation and release---new/dispose, getmem/freemem and other functions differ from the same, memory allocation function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.