Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-05

Is this a reference counting bug?

9 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bihar Anwar

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.

Reply by email

Florian Klaempfl
to Bihar Anwar
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?

FPC and Delphi handle code generation for ref. counted types slightly different, so there might be cases where the ref. counter differs. As long as there is no memory leak when the vars are properly used, we don't consider it as a bug.

Reply by email

Bihar Anwar
to Florian Klaempfl

Florian Klaempfl:

FPC and Delphi handle code generation for ref. counted types slightly different, so there might be cases where the ref. counter differs. As long as

there is no memory leak when the vars are properly used, we

don't

consider it as a bug.

I see, well, there should be explanation about this in FPC doc.

Reply by email

Jonas Maebe
to Bihar Anwar
Bihar Anwar wrote
I see, well, there should be explanation about this in FPC doc.

The reference counting logic is considered to be an opaque implementation detail (whose implementation can change at any time). It is undocumented by design.

Jonas

Reply by email

Bihar Anwar
to Jonas Maebe
Jonas Maebe wrote
The reference counting logic is considered to be an opaque implementation detail (whose implementation can change at any time). It is undocumented by design.

In fact, it was already documented in detail in FPC Language Reference Guide: 3.2.4 Ansistrings; 3.3.1 Arrays: Dynamic Array; 7.6 Reference counting.

Moreover, I've seen many Delphi code snippets out there (e.g. http://www.cs.wisc.edu/~rkennedy/array-delete) rely on reference counting behaviour.

Reply by email

Jonas Maebe
to Bihar Anwar
Bihar Anwar wrote 6 lines
On May 2, 2010 4:24:37 PM, Jonas Maebe wrote: > The reference counting logic is considered to be an opaque implementation detail (whose implementation can change at any time). It is undocumented by design. In fact, it was already documented in detail in FPC Language Reference Guide: 3.2.4 Ansistrings; 3.3.1 Arrays: Dynamic Array; 7.6 Reference counting.

And the last one explicitly states "No assumptions should be made about the number of temporary variables or the time when they are finalized".

Moreover, I've seen many Delphi code snippets out there (e.g. http://www.cs.wisc.edu/~rkennedy/array-delete) rely on reference counting behaviour.

I know, we've had bug reports about that in the past. Such code is simply broken. See e.g. http://bugs.freepascal.org/view.php?id=15526 and http://bugs.freepascal.org/view.php?id=9472

Jonas

Reply by email

Bihar Anwar
to Jonas Maebe: And the last one explicitly states "No assumptions should be made about the number of…

From: Jonas Maebe, May 2, 2010 7:05:46 PM

And the last one explicitly states "No assumptions should be made about the number of temporary variables or the time when they are finalized".

I see, thanks Jonas for noting that statements.

I know, we've had bug reports about that in the past. "Such code is simply broken". See e.g. http://bugs.freepascal.org/view.php?id=15526 and http://bugs.freepascal.org/view.php?id=9472

Thanks also for the your emphatic statement, and the links, of course.

Reply by email

Vinzent Höfler
to Bihar Anwar: In fact, it was already documented in detail in FPC Language Reference Guide: 3.2.4…

Bihar Anwar

Moreover, I've seen many Delphi code snippets out there (e.g. http://www.cs.wisc.edu/~rkennedy/array-delete) rely on reference counting behaviour.

Great. I once wrote code that relied on Turbo Pascal's heap list structure internals.

Shall I post a bug report that this doesn't work anymore in FreePascal (and not even in BP7, IIRC) or shall I simply write it off as intentionally compiler-dependent and non-portable?

Vinzent.

GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

Reply by email