Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 743207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:50:10+00:00 2026-05-14T08:50:10+00:00

I need to execute my code after finalization of SysUtils unit. I’ve placed my

  • 0

I need to execute my code after finalization of SysUtils unit.

I’ve placed my code in separate unit and included it first in uses clause of dpr-file, like this:

project Project1;

uses
  MyUnit,    // <- my separate unit
  SysUtils,
  Classes,
  SomeOtherUnits;

procedure Test;
begin
  //
end;

begin
  SetProc(Test);
end.

MyUnit looks like this:

unit MyUnit;

interface

procedure SetProc(AProc: TProcedure);

implementation

var
  Test: TProcedure;

procedure SetProc(AProc: TProcedure);
begin
  Test := AProc;
end;

initialization

finalization
  Test;
end.

Note that MyUnit doesn’t have any uses.

This is usual Windows exe, no console, without forms and compiled with default run-time packages. MyUnit is not part of any package (but I’ve tried to use it from package too).

I expect that finalization section of MyUnit will be executed after finalization section of SysUtils. This is what Delphi’s help tells me.

However, this is not always the case.

I have 2 test apps, which differs a bit by code in Test routine/dpr-file and units, listed in uses. MyUnit, however, is listed first in all cases.

One application is run as expected: Halt0 -> FinalizeUnits -> …other units… -> SysUtils’s finalization -> MyUnit’s finalization -> …other units…

But the second is not. MyUnit’s finalization is invoked before SysUtils’s finalization. The actual call chain looks like this: Halt0 -> FinalizeUnits -> …other units… -> SysUtils’s finalization (skipped) -> MyUnit’s finalization -> …other units… -> SysUtils’s finalization (executed)

Both projects have very similar settings. I tried a lot to remove/minimize their differences, but I still do not see a reason for this behaviour.

I’ve tried to debug this and found out that: it seems that every unit have some kind of reference counting. And it seems that InitTable contains multiply references to the same unit. When SysUtils’s finalization section is called first time – it change reference counter and do nothing. Then MyUnit’s finalization is executed. And then SysUtils is called again, but this time ref-count reaches zero and finalization section is executed:

Finalization: // SysUtils' finalization
5003B3F0 55               push ebp          // here and below is some form of stub
5003B3F1 8BEC             mov ebp,esp
5003B3F3 33C0             xor eax,eax
5003B3F5 55               push ebp
5003B3F6 688EB50350       push $5003b58e
5003B3FB 64FF30           push dword ptr fs:[eax]
5003B3FE 648920           mov fs:[eax],esp
5003B401 FF05DCAD1150     inc dword ptr [$5011addc] // here: some sort of reference counter
5003B407 0F8573010000     jnz $5003b580     // <- this jump skips execution of finalization for first call
5003B40D B8CC4D0350       mov eax,$50034dcc // here and below is actual SysUtils' finalization section
...

Can anyone can shred light on this issue? Am I missing something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:50:10+00:00Added an answer on May 14, 2026 at 8:50 am

    I was able to find a reason and I feel myself a bit stupid now 🙂

    My second test application have a static reference to DLL, which was compiled with RTL.bpl (it’s empty, except for references to SysUtils and having 1 simple routine). So, since DLL is statically linked, it is initialized before any code from exe have chances to run.

    That’s it:

    DLL’s System -> DLL’s SysUtils -> exe’s System (skipped) -> MyUnit -> exe’s SysUtils (skipped) -> etc

    Finalizations are in reverse order, leading to execution of MyUnit before SysUtils.

    Solution: require to include MyUnit first in all projects.

    (oh, how I wish to have a time-machine to travel back in time and force somebody to add OnBeforeMMShutdown event 😀 )

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 367k
  • Answers 367k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That's because integer values in Excel are imported as floats… May 14, 2026 at 4:46 pm
  • Editorial Team
    Editorial Team added an answer This is basically a type error. When Result is a… May 14, 2026 at 4:46 pm
  • Editorial Team
    Editorial Team added an answer As soon as it's possible to develop two different projects… May 14, 2026 at 4:46 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.