Order of Delphi Initialization

Source: Internet
Author: User

First, the storage structure:

The initialization unit is stored in an array of initcontext.inittable^. In Unitinfo, where Unitinfo is stored as an array, where inittable is defined as:

Where inittable is defined as the type of packageinfo, take a look at the definition of PackageInfo:

You can see that the unitinfo in PackageInfo is defined as the punitentrytable type, and then see the definition of punitentrytable:

You can see that the unit routines are stored in an array and can store up to 10 million cell routines, with Init and finit two entry functions in each routine function, where Init represents the entry address of the initialization routine, Finit is the entry address of the finalization routine;

Conclusion: Delphi initialization routine functions are stored in a sequential list;

Second, initialize the sequence of routine execution:

The initialization routine executes the function in the Initunits function of the System.pas cell:

Conclusion: Initialization execution is a sequential loop execution process, from 0 to Count–1 in the order of execution;

The execution of the finalization routine is in the finalizeunits function of the System.pas unit:

Conclusion: Finalization execution is a reverse loop execution process, from Count-1 to 0 the reverse order of execution;

Third, initialize the sequence of routine search:

The search order of the unit routines is the compiler completes, cannot see the code, but can write some small demo to guess its search order;

If Unit1 is referenced in DPR, then Unit2 is referenced in Unit1, then Unit2 is referenced in Unit3, then initialization is executed Unit3, Unit2, and finally Unit1;

If Unit1 is referenced sequentially in DPR, Unit2, UNIT3, but Unit1 is not referenced in Unit2,unit2, then UNIT3 is executed initialization, UNIT1, and finally Unit2;

conclusion: first from DPR the first element in the file is loaded as a , when attempting to load a the initialization , look first at a the uses section, where the uses not divided interface part or implementation? part, only in order of precedence, when found a have uses Unit , for example, the cell B is referenced in turn and C , you will first set a press the stack, and then process B , if B other units are referenced and treated in the same way as a , if no other cell is referenced, the B routines are saved to unitentrytable (If the unitentrytable already has a B in the The unit is not saved), and then the C is processed in the same way , when a after the referenced units are processed, a eject from stack and save to unitentrytable the other units are processed sequentially;

Order of Delphi Initialization

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.