Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a bug that attempts to compile this code results in IDE terminating or the compiler failing to run?

Beware of Exit command usage in inline functions! I have been using Delphi XE3 here.

Symptom

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;

Questions

  1. Is this a compiler bug?
  2. If this a bug, is there a workaround to safely exit from an inline function?
like image 415
Astaroth Avatar asked Jun 07 '15 09:06

Astaroth


2 Answers

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.

like image 101
David Heffernan Avatar answered Oct 25 '22 04:10

David Heffernan


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;
like image 30
SilverWarior Avatar answered Oct 25 '22 03:10

SilverWarior