[fpc-pascal] library (win32 dll) finalization section - what is allowed and what not?

Bernd Kreuss prof7bit at googlemail.com
Mon Oct 4 15:28:20 CEST 2010


Hi,

I'm currently fighting with some problems that seem to have to do with
the order in which things are done when a library is unloaded.

For example I have written a little exception handler unit that installs
its handler function with AddVectoredExceptionHandler() from within the
initialization section and uninstalls it in the finalization section.
Now I seem to have discovered that it matters in which order I use my
units: when I use my exception handler as the last unit in my library it
will not crash on unload but if it is the first unit it will reliably
crash with c0000005 somewhere in ntdll.dll (sometimes i can catch and
print it while the main application (3rd party, closed source) is still
running but sometimes the app even disappears instantly).

Also it seems that I am risking more crashes *sometimes* and totally
non-deterministic when I use things like
OutputDebugString(PChar(Format(...))) in the finalization section,
sometimes this happens even without the Format() but sometimes it works
(it will output the string but then immediately crash. Might this be the
PChar that becomes invalid too early during unload?)

Also things like TProcess.Terminate; TProcess.Free will reliably provoke
a crash when done during finalization and in general it seems that also
all my TThreads are already stopped (but not freed) at this point, but i
also can't free them anymore at this point.

Currently I am poking around in the dark and doing some trial and error
and try to leave this section alone (as much as possible). Is there
anything I could read that would tell me clearly what is actually
allowed here and in which order things are uninitialized so I can see
myself what must fail and why and what can be safely done?

Bernd




More information about the fpc-pascal mailing list