[fpc-devel] Compiler bug?

Paul van Helden paul at planetgis.co.za
Sun Jan 31 08:41:32 CET 2010


Hi All,

I hope I'm doing something really dumb, but I have to say it is stuff 
like this that is really disheartening to me. I've been using 
FPC/Lazarus for a year now and I have to report that my rate of 
development (compared to Delphi) has slowed down significantly...

The following simple program shows that calling an overloaded method 
from its "twin" returns garbage:

program project1;

{$mode objfpc}{$H+}

type

   TMyClass = class
   public
     function GetValue_not_overloaded: Integer;
     function GetValue_overloaded: Integer; //overload; -- nice feature 
not having to specify "overload"!
     function GetValue_overloaded(SomeValue: Integer): Integer; //overload;
   end;

function TMyClass.GetValue_not_overloaded: Integer;
begin
   Result:=1;
end;

function TMyClass.GetValue_overloaded: Integer;
begin
   Result:=2;
end;

function TMyClass.GetValue_overloaded(SomeValue: Integer): Integer;
begin
   case SomeValue of
     1: Result:=GetValue_overloaded; // compiler warning: "Function 
result variable does not seem to be initialized" (!)
     2: Result:=GetValue_not_overloaded;
   else
     Result:=3;
   end;
end;

var
   C: TMyClass;
   R: Integer;
begin
   C:=TMyClass.Create;
   R:=C.GetValue_overloaded; // direct call works
   WriteLn('Correct value: ', R);
   R:=C.GetValue_overloaded(2); // overloaded function calls non 
overloaded function, works
   WriteLn('Correct value: ', R);
   R:=C.GetValue_overloaded(1); // should return 2, I'm getting random 
numbers starting with 422...
   WriteLn('Random value: ', R);
   ReadLn;
end.

I have tested this with Lazarus 0.9.28.2 (release) and its FPC 2.2.4, a 
fresh build of Lazarus from SVN and FPC 2.4.1 as well as FPC 2.5.1 from 
SVN. The only difference is the actual incorrect value returned! The 
value isn't really random, it is the same each time a specific binary is 
run and always start with 422...
FPC 2.2.4: 4221677
FPC 2.4.1: 4223590
FPC 2.5.1: 4220774

Now (assuming I'm not doing something dumb) if this had been a bug in 
FPC trunk I can understand, but how can such a (common?) use case not 
have been spotted over all these versions?

Perhaps it is bad form to use overloaded functions in such a way, and 
perhaps I'm just frustrated because I'm using an old version of Delphi 
to debug my code, then when I compile it in Lazarus my app implodes. I 
like the latest debugger improvements but I still cannot inspect an 
array element. (If there is a way, please let me know).

Regards,

Paul.



More information about the fpc-devel mailing list