[fpc-devel] ref count issue with out param

Sven Barth pascaldragon at googlemail.com
Sat Jun 13 09:56:44 CEST 2015


On 13.06.2015 06:04, Martin Frb wrote:
> program Project1;
>
> procedure Foo1(a: AnsiString; out b: AnsiString);
> begin
>    WriteLn(length(a));  WriteLn(length(b));
>    b := 'a';
> end;
>
> procedure Foo2(out a: AnsiString; b: AnsiString);
> begin
>    WriteLn(length(a));  WriteLn(length(b));
>    b := 'a';
> end;
>
> const x: AnsiString = 'abcde';
> var s1: AnsiString;
> begin
>    s1 := copy(x,2,3)+'x';
>    Foo1(s1,s1);
>
>    s1 := copy(x,2,3)+'x';
>    Foo2(s1,s1);
>
>    ReadLn;
> end.

The answer is simple: Don't pass a variable you pass as an out-parameter 
also as a by-value or - even worse - const parameter. This is by design 
and an error in /your/ code.

Regards,
Sven



More information about the fpc-devel mailing list