When a function, process, or method defines a string variable, because we know that the variable defined in the function, process, or method is a local variable, its memoryIt is allocated in the stack, but there is a small detail here, we should pay attention to,A local string variable with a size of 4 bytes. The 4 characters allocated in the stackIs it all about string variables?
The answer is no. What it saves in the stack is the size of the character variable and the value of the character variable.Heap memoryAddress !, That is to say, the value of this character variable is not in the corresponding stack, but in the heap! We know that the memory allocated on the stack is processed by programmers.Released fromYou have not processed the memory occupied by this variable! Why?
Here we will talk aboutThe reference counting principle of the compiler is precisely because it saves us a lot of trouble (of course, in some cases we are still careful about it,As mentioned below), referenceIn fact, the general expression of counting is that this memory is referenced by several variables, so it counts the number of the memory (which may be incorrectly expressed, but in order to better understand it), yes1 is 1, and 2 is 2.Push. When a string variable is used up, the compiler automatically reduces its reference count by 1. If the reference count is 0The memory space occupied by this character variable is released. It should be noted that under what circumstances will the reference count occur? If a string variable is defined and assigned a value, the reference count of this string variable is 1. If we define another string variable, assign the value of the first character variable to the second character variable, which will trigger the reference count and change to 2. Note, at this time, the editor did not copy the value of the first character variable to the second character variable, but gave it the address pointer of the value of the first character variable! That is to say, the values of the first and second characters are in the same memory block !. However, when we access character variables by accessing array elements or modifying one of the preceding character variables, "write copy" is triggered. then the two string variables start to use different memory usage. This is because it does not have to be bothered by programmers. It is all done automatically by the compiler. So when should we be careful?
This is related to the DLL. We know that when the EXE program calls the DLL, both the EXE and the DLL use their own memory manager, and the trouble lies in this, if we return a string when calling an input function of DLL in EXE, we know that the reference count of this string is 1. After EXE completes using this string, it will reduce the reference count by 1, that is 0. By convention, the compiler clears and releases the memory occupied by the string, OK, and everything goes well, so it is time for the DLL to process it, as mentioned above, the EXE and DLL have their own memory managers. At this time, the DLL does not know that the EXE has released the string memory and needs to access the released string. The string does not exist, where to visit? So there is a problem with the program. Obviously this is what we don't want to see. When using DLL, we can have a variety of solutions to avoid this problem.
1. Use memory ing files
2. Both the DLL unit and the EXE unit reference the sharemem unit.
3. Do not use the string type (Note: The reference counting mechanism is also available for dynamic arrays, interfaces, and Variant Types). Use the pchar type instead.
Delphi-memory allocation for strings