Typecasting interfaced object to TObject and vice versa

Hello (again)!

Another interface-related problem (or, rather, the same problem in other

Typecasting interfaces to TObject doesn't seem to work at all the way I
would like it to. Consider the following simple declarations:

ITest = interface
  procedure Test(testObj: ITest);

TMyClass = class(TInterfacedObject, ITest)
  procedure Test(testObj: ITest);

Then the test code

procedure TMyClass.Test(testObj: ITest);
var asObj: TObject;
  asObj := TObject(testObj);

leads to that the incoming testObj will be freed. Can anyone tell me why?!?

This has the practical implication that any procedure that accepts
parameters of some interface type can't do anyhthing else than operate on
the interfaced object using only its declared interface. Additional
processing of the object, such as inserting it to a set for
TObject-objects, will cause the object to be freed, as will anything that
requires typecasting the incoming parameter to a TObject. The same seems to
apply for typecasting any TObject descendant to any interface.

As TObject is the base class for every class, including TInterfacedObject,
I find it strange that an actual instance of TInterfacedobject can't be
typecast to TObject...

I specifically want to write interfaces with procedures that accept
interfaced objects as parameters. The specific instances will always be
TObject descendants, so I would like to be able to use them as such...

Explanations and possible workarounds greatly appreciated!!!

Kim Johnsson            kjohn...@abo.fi                http://www.abo.fi/~kjohnsso
Work    +358-2-4101784  Elementum Oy, Artillerigatan 4D, 20520 ?bo, Finland
Home    +358-2-2516805  Sirkkalagatan 36 A 18, 20700 ?bo, Finland