Beware of Exit
command usage in inline functions! I have been using Delphi XE3 here.
In certain circumstances, when a call is made to an inline function that contains Exit
command, and the return value of the inline function is used directly in WriteLn()
, the compiler reports an error message,
"dcc" exited with code 1.
or even worst, the Delphi IDE terminates without any confirmation.
function ProcessNumber(const iNumber: Integer): Boolean; inline;
begin
if iNumber = 0 then begin
Result := False;
Exit;
end;
// some code here ...
Result := True;
end;
procedure Test;
begin
writeln( ProcessNumber(0) );
end;
begin
Test;
ReadLn;
end.
However, if the return value of the inline function is stored in a variable, and then the variable is used in WriteLn()
, the problem does not occur.
procedure Test;
var
b: Boolean;
begin
b := ProcessNumber(0);
writeln(b);
end;
This is certainly a bug. It occurs in all the IDE versions that I tested, XE3, XE7 and XE8. I honestly don't think there's a lot you can do. For me the IDE terminates on compilation every time. I think you'll just have to write the code in a way that does not lead to IDE crashes.
You can use the IDE option that forces compilation to use msbuild. This puts the compilation into a separate process and so ensures that the IDE won't crash. It won't help you much though because although your IDE will not keep dying, you still won't be able to compile your program!
When you build with msbuild, you get an error of this form:
error F2084: Internal Error: GPFC00000FD-004D3F34-0
The GPF stands for General Protection Fault, that is an memory access violation. This presumably is an unhandled exception that is killing the IDE when the compilation is performed in process.
My advice is that you submit a bug report to Quality Portal. That is the only way to get the defect fixed. Although do not expect a fix ever to come to XE3.
One workaround that you could use here is to reverse the if conditional implementation and thus avoid using of Exit command altogether.
So instead of using
function ProcessNumber(const iNumber: Integer): Boolean; inline;
begin
if iNumber = 0 then begin
Result := False;
Exit;
end;
// some code here ...
Result := True;
end;
use
function ProcessNumber(const iNumber: Integer): Boolean; inline;
begin
if iNumber <> 0 then begin
// some code here
Result := True;
end;
else
Result := False;
//No exit needed here as this is already at the end of your method
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With