This program:

This program:


{$APPTYPE CONSOLE}

uses
System.SysUtils;

type
TRec1 = record
X1: NativeInt;
end;

TRec2 = record
X1: NativeInt;
X2: NativeInt;
end;

function GetRec1: TRec1;
begin
Result.X1 := 1;
raise Exception.Create('');
end;

function GetRec2: TRec2;
begin
Result.X1 := 1;
raise Exception.Create('');
end;

procedure Main;
var
Rec1: TRec1;
Rec2: TRec2;
begin
Rec1 := Default(TRec1);
Writeln(Rec1.X1);
try
Rec1 := GetRec1;
except
end;
Writeln(Rec1.X1);

Rec2 := Default(TRec2);
Writeln(Rec2.X1);
try
Rec2 := GetRec2;
except
end;
Writeln(Rec2.X1);
end;

begin
Main;
Readln;
end.


Outputs


0
0
0
1

It's kinda lame that assignments of function return values can happen when the function raises an exception.

It happens because large return types are not returned by value, rather they are handled as implicit var parameters.

https://stackoverflow.com/questions/46466691/delphi-tokyo-exception-prevents-setting-function-result
https://stackoverflow.com/questions/46466691/delphi-tokyo-exception-prevents-setting-function-result

Comments

  1. David Heffernan you don't say :D But it shows how it works.

    ReplyDelete
  2. Alexander Benikowski Indeed. Special optimisation of for loops. But the code you showed doesn't initialise the return value which is obviously a mistake. The issue I am highlighting is with code that is perfectly reasonable.

    ReplyDelete

Post a Comment