1

I have many units with "finalization" sections in my Delphi 7 projects.

How do I make sure to execute some code last?

I tried to write a "finalization" section in the .dpr file but it does not compile.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
user382591
  • 1,320
  • 5
  • 19
  • 39
  • 3
    Initialization & Finalization come from ancient Pascal. Usually they are used to do global things, like class un/registration etc... But this is not so OOP style. Try to avoid them, a better approach is to use Object/Class methods to initialize/deinitialize things. – Marcodor Jul 10 '12 at 14:28
  • FWIW, the newest Delphi has class constructors and destructors which are guaranteed to run before the first access to the class and after the last access to the class respectively. They can often replace the initialization and finalization sections completely. But I don't think the order in which they are called is defined. – Rudy Velthuis Jul 10 '12 at 21:28

1 Answers1

5

There's a good explanation on the finalization ordering for code in Unit finalization order for application compiled with run-time packages - the questioner came up with a reasonable way to ensure that his ordering worked - add the module at the start of the uses clause of the project, and ensure that the module in question does not use any other units.

Edit: Took David Heffernan's addendum on-board.

Community
  • 1
  • 1
Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122