[fpc-pascal] Interface, _AddRef, _Release, etc.
Jonas Maebe
jonas.maebe at elis.ugent.be
Thu Sep 15 14:18:55 CEST 2011
On 14 Sep 2011, at 23:17, Marcos Douglas wrote:
> BUT, if I create my TFoo without refcount should works, right?
Only in some circumstances. These circumstances depend on when the
compiler allocates temporary interface instances, and when these are
finalized.
> ... but, does not work. In Delphi 7 yes, but no in FPC.
It also "works" in FPC if you remove heaptrc. What happens is
a) the compiler creates a temporary copy of the interface instance at
some point in the code
b) you free the underlying object and heaptrc overwrites its memory
with 0xf0f0f0f0
c) at the end of the routine, the compiler tries to finalize the
temporary interface instance. This crashes because the interface
instance no longer contains valid data
Without heaptrc, the example happens to work because the freed memory
of the instance hasn't been reallocated and overwritten yet. It's not
safe in general though.
The answer is: the hack with returning -1 from _AddRef and _Release,
while documented by Embarcadero, is unsafe because it depends on
implementation details. Do not use it. Instead, if you do not want
reference-counted interfaces, use {$interfaces corba} instead as
mentioned earlier in this thread.
Jonas
More information about the fpc-pascal
mailing list