dwscript

DWScript: TdwsGuardianThread.Finalize freeze when inside a DLL


When dwsExprs is added to a DLL, his finalization section freeze. Actaully TdwsGuardianThread.Finalize is frozen on the line guardian.WaitFor.

To demonstrate the issue, I made a sample DLL, which is empty and only include dwsCript. I also made a test application loading the DLL and freeing it immediately. On exit the application freeze as I said above (Put a breakpoint on guardian.WaitFor to see that.

Sample DLL:

library DlltestDll;

uses
  dwsExprs;

{$R *.res}

begin
end.



Sample application:

program DllTest;

{$APPTYPE CONSOLE}

{$R *.res}

uses
    Windows;

var
    HDll : HMODULE;
begin
    HDll := LoadLibrary('DllTestDll.dll');
    FreeLibrary(HDll);
end.

I'm using XE4 and DWScript extracted from SVN repository may 26.

Any help appreciated.


Solution

  • The solution as DWScript is currently is the workaround I described in the comments above:

    Add an exported function in the DLL which calls TdwsGuardianThread.Finalize and call that exported function from the main application before unloading the DLL. Later when dwsExprs finalization section is called, the guardian thread is already stopped and nothing freeze anymore



    The sample DLL now looks like:

    library DlltestDll;
    
    uses
      dwsExprs;
    
    {$R *.res}
    
    procedure Finalize; stdcall;
    begin
       TdwsGuardianThread.Finalize;
    end;
    
    exports
       Finalize;
    
    begin
    end.
    



    And the sample application is:

    program DllTest;
    
    {$APPTYPE CONSOLE}
    
    {$R *.res}
    
    uses
      Windows;
    
    var
        HDll : HMODULE;
        Finalize : procedure; stdcall;
    begin
        HDll := LoadLibrary('DllTestDll.dll');
    
        @Finalize := GetProcAddress(HDll, 'Finalize');
        Finalize;
    
        FreeLibrary(HDll);
    end.