On 14.11.2010 18:17, Florian Klaempfl wrote:
var
t: TMyInterfacedObject;
i: IMyInterface;
begin
t := TMyInterfacedObject.Create;
try
i := t;
t.Foo;
i.Foo;
finally
t.Free;
end;
end.

==== source end ====

Output is:

==== output begin ====

Foo
Bar
An unhandled exception occurred at $08056B71 :
EInvalidPointer : Invalid pointer operation
$08056B71

==== output end ====

(I don't know currently where exactly that EInvalidPointer comes
from.... even a "t.AddRef" doesn't solve this...)

Releasing TInterfacedObject descendants by Free is a bad idea ;) It
destroys the instance without checking if there are still queried
interfaces.

You're right of course...

The following code flow works:

begin
  t := TMyInterfacedObject.Create;
  i := t;
  t.Foo;
  i.Foo;
end;

But why doesn't the following?

begin
  t := TMyInteracedObject.Create;
  try
    t._AddRef;
    i := t;
    t.Foo;
    i.Foo;
i := Nil; // shouldn't this solve the problem as well? or is this a problem of temp inteface variables?
  finally
    t.Free;
  end;
end;

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to