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

Sven Barth pascaldragon at googlemail.com
Mon Oct 4 17:47:41 CEST 2010


Am 04.10.2010 15:28, schrieb Bernd Kreuss:
> 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

You might want to take a look at the Old New Thing blog from one of the 
Microsoft developers. He has written some interesting things about 
DllMain (finalization sections are called when DllMain entrypoint is 
called with DLL_PROCESS_DETACH as parameter).

Here is a search link (for DllMain) on that blog:
http://blogs.msdn.com/search/SearchResults.aspx?q=dllmain&sections=2905

Regards,
Sven



More information about the fpc-pascal mailing list