Where to call FreeLibrary for DLLs used by dynamic packages?

396 Views Asked by At

I have a dynamically loaded BPL package which uses a third part library who loads a DLL.

After calling UnloadPackage for my BPL package, the application still locks the DLL file.

var
  MyPackageHandle : THandle;
  MyPackageClass : TPersistentClass;
  MyPackageForm : TCustomForm;
begin
  MyPackageHandle := LoadPackage('.\MyPackage.bpl');
  if(MyPackageHandle <> 0) then 
  begin
    try
      MyPackageClass := GetClass('TMyPackageForm');
      if(Assigned(MyPackageClass) then
      begin
        MyPackageForm := TComponentClass(MyPackageClass).Create(nil) as TCustomForm;
        MyPackageForm.ShowModal();
        MyPackageForm.Free();
      end;
    finally
      UnloadPackage(MyPackageHandle);
    end;
  end;
end;

For testing, I'm using the Windows.GetModuleHandle function.

I've tried calling FreeLibrary passing the handle of the DLL and then I'm able to delete the DLL file. I suspect that a FreeLibrary call is missing somewhere in my BPL package or in the third part BPL.

In a condition like the one described above (A dynamically loaded BPL which statically links a third part BPL which uses a DLL), where should the FreeLibrary be executed?

0

There are 0 best solutions below