Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing the initialization order of the unit in Delphi

I'm using Delphi XE7 for developing windows 32 bit application.

My application contains many units, which has an initialization section. I need to initialize one particular initialization section at first.

Is it possible to set the priority? I have tried to write the initialization section in dpr file, but the compiler has rejected this.

Please help me to execute the particular initialization section at first. Thanks in advance.

like image 850
test12345 Avatar asked Aug 30 '18 18:08

test12345


People also ask

How does Delphi initialize a unit?

Delphi first runs the initialization section for all the units the Name unit uses, in order of appearance, starting with the interface units, followed by the implementation units. A unit’s initialization section runs after Delphi runs the initialization section for all used units.

What is an example of unit initialization?

Setting a new memory manager is a common example of a unit that must be initialized first. The order of unit initialization means you cannot know for certain when your unit will be initialized, but all the units used in the interface section will be initialized first.

What is the structure of a Delphi application?

This topic covers the overall structure of a Delphi application: the program header, unit declaration syntax, and the uses clause. Divide large programs into modules that can be edited separately. Create libraries that you can share among programs. Distribute libraries to other developers without making the source code available.

How many times are initialization and finalization called per execution?

And these Initialization and Finalization sections are called only once per execution of the .Exe. It ought to be apparent from the above that the calls to the Initialization/Finalization sections are unrelated to whether any classes in the units are ever instantiated.


1 Answers

In simple terms, initialization sections are executed in the order in which the units are introduced in any uses clauses. But it is a little more complicated than that due to the fact that initialization of a unit is performed only after the initialization of any units that that unit itself references (where they have not already been initialized).

i.e. Given:

program Foo;

  uses
    Unit1,
    Unit2,
    Unit3;

unit Unit1;

interface

  uses
    Unit3;

Then the unit initialization order will be:

Unit3
Unit1
Unit2

This is because Unit1 introduces Unit3, so even though Unit1 is listed first in the dpr uses, Unit3 is in fact initialized first, followed by the initialization of Unit1.

If you remember that the initialization section occurs after any uses clauses in a unit, it does make sense.

Therefore the only way to be absolutely sure of any one unit being initialized before any other is to list it first in the DPR uses clause and for that unit to take no dependencies on any other units (except where those units are not dependent on, or otherwise interfere with, the initialization being performed) .

It need not necessarily be strictly first of course. e.g. if you are using a replacement memory manager (such as FastMM) then this will absolutely need to be the very first unit listed in your dpr uses clause. You simply need to make sure that the unit you need to be initialised before any other (of your units) is then listed before any other unit which might bring your other units in:

program  Foo;

uses
  FastMM,            // MUST be first but won't bring any of 'my' units in, so this is OK

  SysUtils,          // These too are fine coming next because again they don't
  Forms,             // reference 'my' units

  MyInitUnit,        // <- This is where it is important to list 'my' guaranteed first/earliest
                     //     initialisated unit of all 'my' units

  MyFirstAppUnit,    // And now the rest ...
  etc;

Of course, if the unit that you wish to initialise first does need to be initialised before any other, including RTL units (in the same way that FastMM etc need to be) then you would need to reflect that in the dpr uses list by declaring your unit earlier still.

like image 147
Deltics Avatar answered Sep 19 '22 02:09

Deltics