[fpc-pascal] Is this a reference counting bug?

Bihar Anwar bihar_anwar at rocketmail.com
Sun May 2 07:59:19 CEST 2010


I found that the last element of a dynamic array starts with reference count = 2 in FPC, but in Delphi is 1. Is this an FPC bug, or FPC implements reference counting differ from Delphi?

type
  PAnsiRec = ^TAnsiRec;
  TAnsiRec = packed Record
    Ref,
    Len   : SizeInt;
    First : Char;
  end;

const
  AnsiRecLen = SizeOf(TAnsiRec);
  FirstOff   = SizeOf(TAnsiRec) - SizeOf(Char);

procedure Test;
var
  a: array of ansistring;

begin
  SetLength(a, 5);
  a[0] := IntToStr(0);
  a[1] := IntToStr(1);
  a[2] := IntToStr(2);
  a[3] := IntToStr(3);
  a[4] := IntToStr(4);

  WriteLn('a[0] = ', PAnsiRec(Pointer(a[0]) - FirstOff)^.Ref);  // Ref Count = 1
  WriteLn('a[1] = ', PAnsiRec(Pointer(a[1]) - FirstOff)^.Ref);  // Ref Count = 1
  WriteLn('a[2] = ', PAnsiRec(Pointer(a[2]) - FirstOff)^.Ref);  // Ref Count = 1
  WriteLn('a[3] = ', PAnsiRec(Pointer(a[3]) - FirstOff)^.Ref);  // Ref Count = 1
  WriteLn('a[4] = ', PAnsiRec(Pointer(a[4]) - FirstOff)^.Ref);  // Ref Count = 2, why???
end;

begin
  Test;
  ReadLn;
end.



      



More information about the fpc-pascal mailing list