[fpc-pascal] Interface, _AddRef, _Release, etc.

Marcos Douglas md at delfire.net
Thu Sep 15 17:03:59 CEST 2011

On Thu, Sep 15, 2011 at 9:18 AM, Jonas Maebe <jonas.maebe at elis.ugent.be> wrote:
> 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.

Ok, now I understood.

> 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.

I know that is a hack... just was an example.
About CORBA I think did you answer my question before (I asked Graeme
about it). But if no have reference-counted how I release a reference,
just use ref := nil?
I will search -- and test -- about it, thanks.

Marcos Douglas

More information about the fpc-pascal mailing list