Dear all,

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.)

[1] 
https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-best-practices
[2] 
https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibrarya

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:

***

library dummylib;

uses
  Classes;

begin
end.

***

program project1;

uses SysUtils, Types, Classes;

type TLoadLibThread = class(TThread)
  procedure Execute; override;
  var FHandle: THandle;
end;

procedure TLoadLibThread.Execute;
begin
  FHandle := LoadLibrary('dummylib.dll');
end;

var h: THandle = NilHandle;

begin
  with TLoadLibThread.Create(FALSE) do begin
    WaitFor;
    h := FHandle;
    Free;
  end;
  Assert (h <> NilHandle);
  FreeLibrary (h); 
end.

***

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.

Thanks,
Thomas

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to