I in haste tried now to produce this strange behaviour in Delphi compiler where no warning issued
Delphi-Quellcode:
type
TMyClass = class
private
FInitValue: Integer;
public
constructor Create(InitValue: Integer); overload;
function DoCalc(Value: Integer): Integer;
end;
TCont = class
private
FMyClass: TMyClass;
public
procedure Init(Value: Integer);
procedure DeInit;
function Calc(Value: Integer): Integer;
end;
constructor TMyClass.Create(InitValue: Integer);
begin
inherited Create;
FInitValue := InitValue;
end;
function TMyClass.DoCalc(Value: Integer): Integer;
begin
Result := Value * FInitValue;
end;
function TCont.Calc(Value: Integer): Integer;
begin
Result := FMyClass.DoCalc(Value)
end;
procedure TCont.DeInit;
begin
FMyClass.Free;
end;
procedure TCont.Init(Value: Integer);
begin
FMyClass := TMyClass.Create(Value);
end;
function Test1: Integer;
var
C: TCont;
begin
C := TCont.Create;
try
C.Init(10);
Result := C.Calc(5);
C.DeInit;
finally
C.Free;
end;
end;
function Test2: Integer;
var
CE: TCont;
begin
CE := @CE;
CE.Init(11);
Result := CE.Calc(9);
//C.DeInit;
end;
var
A, B: Integer;
begin
A := Test1;
B := Test2;
Writeln(A);
Writeln(B);
Readln;
end.
In Delphi 2010 it does exit silently with no
exception, while Delphi XE8 it run like with correct result, in both cases "CE := @CE" fooled the compiler, also the warning is easily can be detected by the compiler when the variable localized and stares at the compiler in the eyes, but when a function is long enough the compiler might not detect it at all.