[fpc-pascal] Is it posible to implement more than one interface in a class defination?

Sven Barth pascaldragon at googlemail.com
Sun Nov 14 18:27:32 CET 2010


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



More information about the fpc-pascal mailing list