Sign in Join

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

Mailing lists / fpc-pascal / 2015-05

Managed types within records

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Jürgen Hestermann

Hi,

when I have a record which has managed types (i.e. strings), how are they handled by the compiler when I assign one record to the other? Example:

---------------------------------------------------
type MyRecordType = record
                                       X : SizeInt;
                                       S : AnsiString;
                                       end;
var A,B : ^MyRecordType;
begin
new(A);
new(B);
A^.X := 7;
A^.S := 'String in A';
B^ := A^;
Dispose(A);
end;
---------------------------------------------------

What happens to the reference counter of S?
Is it updated correctly (assigned to zero with new, incremented with B^:=A^ and decremented with dispose)?

Reply by email

Sven Barth
to Jürgen Hestermann
"Jürgen Hestermann" wrote 5 lines
Hi, when I have a record which has managed types (i.e. strings), how are they handled by the compiler when I assign one record to the

other?

Show quoted text 22 lines
Example: --------------------------------------------------- type MyRecordType = record X : SizeInt; S : AnsiString; end; var A,B : ^MyRecordType; begin new(A); new(B); A^.X := 7; A^.S := 'String in A'; B^ := A^; Dispose(A); end; --------------------------------------------------- What happens to the reference counter of S? Is it updated correctly (assigned to zero with new, incremented with B^:=A^ and decremented with dispose)?

Yes, they are handled correctly. The compiler inserts a call to a RTL function that does a per-element copy using the record's RTTI instead of doing a memory copy (this is true for all records even if they don't have managed types).

Regards,
Sven

Reply by email