Sign in Join

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

Mailing lists / fpc-pascal / 2010-10

library (win32 dll) finalization section - what is allowed and what not?

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

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

Reply by email

Jonas Maebe
to Bernd Kreuss
Bernd Kreuss wrote 6 lines
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?

I don't know anything about Windows specifically, but FPC always executes the finalization sections of all units in the opposite order compared to how the initialization sections were called. The initialization order is determined by the unit dependencies (processed in the order in which units appear in the uses-clauses, using a depth- first approach), but note that the order is hard to predict if you have circular dependencies.

Jonas

Reply by email

Sven Barth
to Bernd Kreuss: I'm currently fighting with some problems that seem to have to do with the order in which…
Bernd Kreuss wrote 34 lines
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

Reply by email