[fpc-devel] Reference counting interface objects
Jonas Maebe
jonas.maebe at elis.ugent.be
Thu Oct 16 13:52:30 CEST 2014
On 16 Oct 2014, at 12:35, Adriaan van Os wrote:
> Now, it seems (correct me if I am wrong) that theEvent doesn't need
> to be manually released, because it has been declared as an OUT
> parameter rather than a VAR parameter. So, if this is true, this
> information is crucial when writing interface bindings.
Nothing ever needs to be manually released, regardless of whether it's
a VAR or OUT parameter. That's the whole point of automatic reference
counting)
Regarding the effects of the various qualifiers:
* nothing (value): the reference count of the parameter is increased
(I forgot whether it's the caller or callee that does it) on entry and
decreased on exit
* OUT: the reference count of the value that's passed in is decreased
by 1, and the variable that's passed into the procedure is initialized
to "empty" (nil, but that's an implementation detail)
* VAR: nothing happens to the reference count. A reference to the
original variable is passed in, and changing it or reading it has
exactly the same effect as changing/reading the original variable.
* CONST: this is the only tricky one. Again nothing happens to the
reference count, but because you can pass non-lvalues here. In
particular, you can pass a class implementing an interface rather than
the interface itself, which can cause the class to be freed
unexpectedly. See the example at http://docwiki.embarcadero.com/RADStudio/XE7/en/Using_Reference_Counting
. The same can happen with functions returning a class instance
passed to a function expecting a CONST interface parameter. There is
an open bug report about adding a warning for it: http://bugs.freepascal.org/view.php?id=19503
Jonas
More information about the fpc-devel
mailing list