[fpc-devel] Management operators : Copy and Clone confusion...

Thorsten Engler thorsten.engler at gmx.net
Mon Jan 23 16:05:23 CET 2017


It's been a few years since I looked at the relevant code in the FPC
compiler and RTL, so my knowledge may be woefully out of date and/or
degraded by time, but IIRC:

- Copy was called with a source and target pointer and is responsible for
copying everything that's needed from the source to the target.
- AddRef is called with just a single pointer, after the contents of a
variable has been duplicated (simply by copying the contents byte by byte,
not by calling the "Copy" operator code).

Again, IIRC, the reason for both to exist is that there are cases (e.g.
passing an interface parameter) where the callee is responsible for any
lifetime management implications (and the caller might not even be
implemented using fpc). The caller just passes the interface pointer on the
stack (or in a register), which, while it creates a copy of the bytes, does
not actually invoke the copy operator. In cases where reference counting is
required (parameter not marked as const), the callee may decide to simply
use the value that was passed on the stack as the backing for the parameter,
so it calls AddRef on that, without performing another copy.

Given this context, I find the terms AddRef and Copy to be perfectly
adequate, even when applied to types that aren't strictly speaking
performing reference counting; e.g. IIRC, AddRef for a BSTR (ole string type
for windows which does NOT have a reference count) actually involves making
a copy of the string contents and replacing the BSTR pointer.

AddRef here means "this is now an *additional* *reference* to whatever these
bytes might refer to".

Cheers,
Thorsten

> -----Original Message-----
> From: fpc-devel-bounces at lists.freepascal.org [mailto:fpc-devel-
> bounces at lists.freepascal.org] On Behalf Of Michael Van Canneyt
> Sent: Monday, 23 January 2017 19:30
> To: FPC developers' list <fpc-devel at lists.freepascal.org>
> Subject: Re: [fpc-devel] Management operators : Copy and Clone
confusion...
> 
> 
> 
> On Sat, 21 Jan 2017, Florian Klämpfl wrote:
> 
> > Am 19.01.2017 um 10:44 schrieb Michael Van Canneyt:
> >>
> >> At the risk of writing nonsense:
> >>
> >> I would think that a method name should give a clue as to what it
actually
> does.
> >> In that sense AddRef/Clone seems better to me ?
> >
> > Even if it is used by types not being ref. counted? Ref. counted types
> > are only a sub-set of managed types.
> >
> >> (unless Copy does something else besides increasing the ref count ?)
> >>
> >> AddRef is clear to anyone, I think.
> >
> > For non-ref. counted types?
> 
> I didn't know it is used for non-ref. counted types as well.
> For those types, there is no difference between copy/clone then?
> 
> Michael.




More information about the fpc-devel mailing list