Original reproduced in: http://shao198888.blog.163.com/blog/static/238380067201502010141545
1 initialization and finalization
Test method: Start a console program, because the general application has too many unit references, may cause some debugging trouble
After testing, the approximate result may be this
The loading order is related to project and the reference order of each unit
First load the first unit from the project file, such as a, when trying to load a initialization, first look at the uses part of a, where the uses is not divided interface part or implementation part, only in order, When a uses unit is found, for example, in order to refer to cell B and C, before loading a initialization, first load B (here can be understood as a press on the stack, specifically to deal with B), recursively, if the B unit is processed, found that B refers to a, according to the test results, Guess is first to "stack" to see if there is no unit a, if there is, skip the part of a processing, if not, put B into the stack, processing the new reference unit
Or in the initialization, there is an imaginary queue, the beginning is empty, when the project is loaded, the first unit is placed on the queue header, then the first unit is opened, followed by the uses unit, if the queue does not have the cell name, it is appended to the queue tail, if the queue is already there, skip the unit, recursively , and finally, put all the uses of the project into a queue, and then,
The order of the initialization is: backward from the tail of the queue, execute the initialization of each unit sequentially,
Instead, the finalization is executed backwards from the head of the queue.
The top is probably wrong, simply speaking, is a recursive, encountered uses to find the source code, recursion to the source of the uses, if a unit is not uses, then the implementation of the Unit initialization
The statement about the queue is too arbitrary, it should be a simple recursion, my algorithm is not good, do not know how to describe.
Delphi initialization and destruction of initialization and finalization