Script requires fake variable initialization

My program is calling a scriptfunction which in turn calls a delphi function with a var parameter.
When I run the software the scripts throws an error, see attached picture.
Screenshot 2022-05-31 202050
I made a test where one of the scriptfunctions has a fake variable initilization.
This function work with no error.
Here are the two functions :

// runs as it should
function MM_Lotol( FuName: string );
var
d: double;
i: integer;
begin
i := 0; // Fake initilization of i, unneccessary, since i is a var parameter
if Merkmallist.GetMMIdxbyFunctionName( FuName, i ) then begin
d := Merkmallist.Merkmale[i].Lotol;
Result := Merkmallist.Merkmale[i].FormatMMQs( d );
end
else Result := 'MM not found';
end;

// almost same function, throws an error
function MM_Uptol( FuName: string );
var
d: double;
i: integer;
begin
if Merkmallist.GetMMIdxbyFunctionName( FuName, i ) then begin
d := Merkmallist.Merkmale[i].Uptol;
Result := Merkmallist.Merkmale[i].FormatMMQs( d );
end
else Result := 'MM not found';
end;

This is the definition of the called function, its normally compiled in Delphi
function TMerkmallist.GetMMIdxbyFunctionname(Value: string; var mmidx: integer): boolean;
var
i: integer;
begin
Result := false;
mmidx := -1;
if Length(Value) > 0 then
for i := 0 to Count - 1 do begin
if Merkmale[i].Functionname = Value then begin
mmidx := i;
Result := true;
break;
end;
end;
end;

To avoid crashes, I have t initialized parameters to a delphi function.
The parameter might have any value, it is writen by called function no matter what its value is.
But it should not crash.

That's expected, TMS Scripter variables are not strongly typed, all of them are Variants. Thus, they are initialized to NULL.

The scriptfunction was originally created and used in 2010.
It tested the function with an old exe from 2014 and it worked without extra initialization.

Here is an excerpt from a scriptfile dated 28.May 2010 :
It runs without crash.

// untere Toleranzgrenze eines Merkmals unverändert ausgeben
function MM_Lotol( MMCode: string );
var
d: double;
i: integer;
begin
if Merkmallist.GetMMIdxbyMerkmalcode( MMCode, i ) then begin
d := Merkmallist.Merkmale[i].Lotol;
Result := Merkmallist.Merkmale[i].FormatMMQs( d );
end
else Result := 'MM not found';
end;

Do you have a full compilable project reproducing the issue? We can then take a look and see exactly what's going on.

Sorry, the project is not only big but depends on a lot of libraries.
For me it is no problem to add varaiable initialization, I just was not aware of it. From my point a good solution would be to add a warning in the documentation that calling a delphi function with var parameter needs initilization.

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.