6

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.

LU RD
  • 34,438
  • 5
  • 88
  • 296
test12345
  • 367
  • 1
  • 21
  • 3
    You have no control over prioritizing `initialization` sections. The only thing you can do is reorder the units in your `uses` clauses. Make sure the unit for the "one particular initialization section" is earlier in the `uses` list than other units, or is the first unit in the `dpr` file. – Remy Lebeau Aug 30 '18 at 18:41
  • Have you tried putting the unit first in the .Dpr file? And, what Remy says. – MartynA Aug 30 '18 at 18:41
  • It can often be hard to control this, you may to consider solutions that don't rely on initialization order. – David Heffernan Aug 30 '18 at 19:50
  • I ended up adding one unit IntializeFILF, it has two methods AssureFI and AddtoLF. AssureFI initializes stuff like Log files and Tracing Units. All other initialization code first calls that and optionally adds its finalization to AssureLF, no further issues since then.. – FredS Aug 30 '18 at 21:57

2 Answers2

16

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.

Deltics
  • 22,162
  • 2
  • 42
  • 70
0

The initialization sections are executed in the order that the units appear in the uses clause.

So, you may force your unit to initialize first by moving it up in the uses list.

PA.
  • 28,486
  • 9
  • 71
  • 95
  • Not necessarily in that order. See Deltics' answer. – Rudy Velthuis Aug 30 '18 at 19:40
  • What about units in the interface `uses` list v. the one in the implementation one? – MartynA Aug 30 '18 at 20:41
  • I think it works like this: all units are listed in "order of appearance", with duplicates removed. Then each unit on which others depend is listed before the first one that depends on it. This is checked for all units. The resulting list is the order of initialization. – Rudy Velthuis Aug 31 '18 at 08:56