Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-11

Is there a way to execute code after 'initialization' and before 'main', i. e. AddExitProc complement?

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Роман

'Initialization' sections are executed in poorly predictable order, so I want to partially order them according to some form of explicit prioritization and execute before control flow enters main block (so I won't need to run all that initialization by hand, neither I would be able to forget to do that). Like:

unit UnitA;
...
procedure Init;
initialization
   RegisterUnit('UnitA', @Init);
end;
unit UnitB;
...
procedure Init;
initialization
   RegisterUnit('UnitB', @Init, {priority} +1);
end;

...
procedure SortAndInitialize;
...

Can I run SortAndInitialize before 'main' without additional user actions?

Reply by email

Sven Barth
to Роман

Am 28.11.2017 20:04 schrieb "Роман via fpc-pascal" <

:

'Initialization' sections are executed in poorly predictable order, so I want to partially order them according to some form of explicit prioritization and execute before control flow enters main block (so I won't need to run all that initialization by hand, neither I would be able to forget to do that).

If unit A uses unit B in the interface section then unit B's initialization section is always run before A's. For the implementation section the same holds true if there isn't a cycle otherwise it depends on the unit load order.

Like:

unit UnitA;
...
procedure Init;
initialization
  RegisterUnit('UnitA', @Init);
end;
unit UnitB;
...
procedure Init;
initialization
  RegisterUnit('UnitB', @Init, {priority} +1);
end;

...
procedure SortAndInitialize;
...

Can I run SortAndInitialize before 'main' without additional user actions?

No, you can't.

Regards,
Sven

Reply by email

Christo Crause
to Роман: 'Initialization' sections are executed in poorly predictable order, so I want to…

On 28 Nov 2017 9:04 PM, "Роман via fpc-pascal" <

wrote: Can I run SortAndInitialize before 'main' without additional user actions?

Maybe not exactly what you want but you could probably add the SortAndInitialize procedure to the initialization section of a new unit and add this unit as the last unit to your program.

This use case is probably a good candidate for using the "section" modifier to specify initialization sequence. Unfortunately not available in the compiler for code sections AFAIK.

Reply by email