AV/Crash Executing Scripts with Function Pointers and Accessing global variables.

Issue #3 new
Steffen Binas created an issue

Script execution crashes if a function called via a function pointer accesses a global variable.

For convenience I attached a testcase: Copy it into "Tests\SimpleScripts"-Directory and it will crash executing the script (compiles without errors). Tested with the svn-revision R2530 (April 2014).

Some notes:

  1. It will not crash (test passes), if function "Setting" is called directly (not as a function pointer)
  2. It will not crash (test passes), if you execute the content of the "DoIt"-function directly (one nesting level fewer).

type
  TSettingFunction = function(): Boolean;

type
  TFooObject = class
    FValue: Integer;
    constructor Create(A: Integer);
    begin
      FValue := A;
    end;
    function Value: Integer;    
    begin
      Result := FValue; 
    end;
  end;

var AFoo: TFooObject = nil;
function Foo: TFooObject;
begin
  if AFoo = nil then
    AFoo := TFooObject.Create(42);
  Result := AFoo;
end;

function Setting(): Boolean;
begin
  PrintLn(IntToStr(Foo.Value));
  Result := True;
end;

procedure DoIt ();
begin
  var SettingFunction: TSettingFunction = Setting;
//  Setting(); // Works 
  SettingFunction(); // Crash   
end;

DoIt();

Comments (3)

  1. Steffen Binas reporter

    I rerun the test with current version (ee2dc49) and there was no AV anymore, just a scripting-runtime error. But the main problem persists, that the code doesn't work when called indirectly.

  2. Eric Grange repo owner

    Do you have some sample code for the Delphi side? or maybe a variation of one of the tests in UdwsUnitTests?

  3. Log in to comment