FreeLibrary Hangs when releasing firemonkey dll

  

I have very strange problem and I hope somebody could explain it to me.

I created VCL app and FMX dll (just a dll with FMX form). I need to load DLL dynamically using LoadLibrary and FreeLibrary. There are two cases.

In first case my code is:

procedure TForm2.FormCreate(Sender: TObject);
begin
Path := ExtractFilePath(Application.ExeName)+ ‘plugins’;
FLibHandle := LoadLibrary(PWideChar(Path + ‘Plugin.dll’));
if FLibHandle > 32 then
begin
@FOpenForm := GetProcAddress(FLibHandle, ‘TestOpenGUI’) ;
@FCloseForm := GetProcAddress(FLibHandle, ‘TestCloseGUI’) ;
end;
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
FreeLibrary(FLibHandle);
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
FOpenForm;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
FCloseForm;
end;

The problem here is if I click button1 and then button2 (Just open the dll form and then close it) and close the app it will hang on FreeLibrary. I have to termiante the app (CTRL+F2).

In second case my code is:

procedure TForm2.Button1Click(Sender: TObject);
begin
FOpenForm;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
FCloseForm;
end;

initialization
Path := ExtractFilePath(Application.ExeName)+ ‘plugins’;
FLibHandle := LoadLibrary(PWideChar(Path + ‘Plugin.dll’));
if FLibHandle > 32 then
begin
@FOpenForm := GetProcAddress(FLibHandle, ‘TestOpenGUI’) ;
@FCloseForm := GetProcAddress(FLibHandle, ‘TestCloseGUI’) ;
end;

finalization
FreeLibrary(FLibHandle);

Library will be loaded and then if I click button1 and then button2 and close the app, library will be released without any problem. In this case everything works just fine.

I am a little bit confused and I don’t know what am I doing wrong.

Thanks for your help.

Comments are closed.