Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not responding while compiling this function?

I tried with Delphi XE and I got Not Responding while compiling. Does it work in your computer or is there something wrong with the function?

function Test(const FileName: string;
  const Force: boolean = false): boolean;
var
  IsAllowed: boolean;
begin
  result := false;
  if FileExists(FileName) then
  begin
    try
      if (Force) then
      begin
        result := false;
        exit;
      end;
    finally
      if IsAllowed then
        DeleteFile(FileName);
    end;

    try
      result := true;
    except
      result := false;
    end;
  end;
end;
like image 766
user Avatar asked May 12 '11 09:05

user


1 Answers

It compiles on my computer. Though I get the warning W1036 Variable 'IsAllowed' might not have been initialized.

Update: I can reproduce the hang when I include Windows in the uses clause. Subbmitted to Quality Central: QC93806.

program hang_test;

{$APPTYPE CONSOLE}

uses
  // Windows, // uncomment to include Windows -> hang on compile
  SysUtils;

function Test(const FileName: string; const Force: boolean = false): boolean;
  // your function here

begin
  try

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

It looks like a bug; you should report it in the Quality Central.

Update 2: Minimal case which reproducibly hangs the compiler:

function HangCompiler: Boolean;
begin
  try
    Exit; // 1. exit from a try..finally
  finally
    DeleteFile(''); // 2. inlined function call in finally (include Windows to inline)
  end;
  // 3. try..except
  try
    Result := True;
  except
    Result := False;
  end;
end;
like image 180
Ondrej Kelle Avatar answered Nov 15 '22 06:11

Ondrej Kelle