[fpc-pascal] LoadLibrary, FreeLibrary & Threads (win32)
fpc.2021 at t-net.ruhr
Thu Jun 22 23:12:59 CEST 2023
I have spent 2 days now tracking an access violation in my app which I couldn't debug well because it occured somewhere where no debug info was available, but obviously during a call to FreeLibrary.
I don't know whether I am allowed to do this or not, but as far as I have read from the Microsoft docs about using libraries [1,2] I didn't find a note that my approach is forbidden. (Of course, I may have overread it.)
What I did is to call LoadLibrary in a different thread than the main thread. The corresponding call to FreeLibrary on the other hand was done in the main thread.
Question 1: Is this legal on Windows? If not, we're finished here and you can skip the rest ;)
However, if it *is* legal, I'd need some help with FPC. Here's a minimal example:
uses SysUtils, Types, Classes;
type TLoadLibThread = class(TThread)
procedure Execute; override;
var FHandle: THandle;
FHandle := LoadLibrary('dummylib.dll');
var h: THandle = NilHandle;
with TLoadLibThread.Create(FALSE) do begin
h := FHandle;
Assert (h <> NilHandle);
The call to FreeLibrary causes an "access violation writing to address $14".
I also tried to follow the advice in the "multithreaded application tutorial" in the wiki ("initialize the FPC's threading system by creating a dummy thread"), but this doesn't make any difference.
I assume that I have some obviouy error and am just too blind to see, so I'd appreciate someone pointing me to it.
More information about the fpc-pascal