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.