Why casting interface as Tobject will result in a different reference?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Why casting interface as Tobject will result in a different reference?

Dennis
I defined a generic TList<IMyInterface> //CORBA style interface

var
    aInt : IMyInterface;
   aObj : TObject;
begin
    aInt :=  MyList.Items[0];
    aObj := aInt as TObject;

   writeln(  IntToHex(LongWord(aInt),4));

   writeln(  IntToHex(LongWord(aObj),4)); //this will output a different
Hex value from previous writeln


//This is causing a problem to me because I later

   MyList.Remove(aObj as IMyInterface);//will fail because it is not the
same as aInt and thus fail to locate the item to remove


Am I missing some fundamental understanding of interface reference?

Dennis

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Why casting interface as Tobject will result in a different reference?

Michael Van Canneyt


On Fri, 26 May 2017, Dennis wrote:

> I defined a generic TList<IMyInterface> //CORBA style interface
>
> var
>    aInt : IMyInterface;
>   aObj : TObject;
> begin
>    aInt :=  MyList.Items[0];
>    aObj := aInt as TObject;
>
>   writeln(  IntToHex(LongWord(aInt),4));
>
>   writeln(  IntToHex(LongWord(aObj),4)); //this will output a different
> Hex value from previous writeln
>
>
> //This is causing a problem to me because I later
>
>   MyList.Remove(aObj as IMyInterface);//will fail because it is not the
> same as aInt and thus fail to locate the item to remove
>
>
> Am I missing some fundamental understanding of interface reference?

Why do you think the interface and the object are the same ? They are not.
An interface is just a bunch of method pointers, and an object has instance
data associated with it. Hardly the same thing, I would think ?

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Why casting interface as Tobject will result in a different reference?

Ryan Joseph
In reply to this post by Dennis

> On May 26, 2017, at 4:06 PM, Dennis <[hidden email]> wrote:
>
>  writeln(  IntToHex(LongWord(aInt),4));
>
>  writeln(  IntToHex(LongWord(aObj),4)); //this will output a different Hex value from previous writeln

I think an object and interface are indeed not the same memory and you need to use Supports() to get a reference to the interface from the object. The interface reference can be cached and used later or you need to call Supports every time which does a string compare and is slow.

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Why casting interface as Tobject will result in a different reference?

Graeme Geldenhuys-6
In reply to this post by Dennis
On 2017-05-26 10:06, Dennis wrote:
> Am I missing some fundamental understanding of interface reference?

Bottom line is, NEVER mix Objects and Interfaces. Program for the one or
the other.

Regards,
   Graeme

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Why casting interface as Tobject will result in a different reference?

Free Pascal - General mailing list
In reply to this post by Michael Van Canneyt
2017-05-26 11:08 GMT+02:00 Michael Van Canneyt <[hidden email]>:

>
>
> On Fri, 26 May 2017, Dennis wrote:
>
>> I defined a generic TList<IMyInterface> //CORBA style interface
>>
>> var
>>    aInt : IMyInterface;
>>   aObj : TObject;
>> begin
>>    aInt :=  MyList.Items[0];
>>    aObj := aInt as TObject;
>>
>>   writeln(  IntToHex(LongWord(aInt),4));
>>
>>   writeln(  IntToHex(LongWord(aObj),4)); //this will output a different
>> Hex value from previous writeln
>>
>>
>> //This is causing a problem to me because I later
>>
>>   MyList.Remove(aObj as IMyInterface);//will fail because it is not the
>> same as aInt and thus fail to locate the item to remove
>>
>>
>> Am I missing some fundamental understanding of interface reference?
>
>
> Why do you think the interface and the object are the same ? They are not.
> An interface is just a bunch of method pointers, and an object has instance
> data associated with it. Hardly the same thing, I would think ?

The idea itself is valid, cause "(IMyInterfaceVar as TObject) as
IMyInterface = IMyInterfaceVar" is true if and only if IMyInterface is
a COM interface. If IMyInterface really is a CORBA interface as Dennis
wrote then the compiler should already have complained at the "aObj :=
aInt as TObject" line as this type of conversion is *not* supported by
CORBA interfaces (cause they don't provide QueryInterface() which is
needed for this). At least for me the compiler *did* complain (both
3.0.2 and 3.1.1) when I tested this.
So the question is: is IMyInterface indeed a CORBA interface? If so
then why does the compiler not complain? If not then why doesn't it
work? For both cases a full example would be needed to investigate
this.

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Why casting interface as Tobject will result in a different reference?

Dennis Poon


Sven Barth via fpc-pascal wrote:

> The idea itself is valid, cause "(IMyInterfaceVar as TObject) as
> IMyInterface = IMyInterfaceVar" is true if and only if IMyInterface is
> a COM interface. If IMyInterface really is a CORBA interface as Dennis
> wrote then the compiler should already have complained at the "aObj :=
> aInt as TObject" line as this type of conversion is*not*  supported by
> CORBA interfaces (cause they don't provide QueryInterface() which is
> needed for this). At least for me the compiler*did*  complain (both
> 3.0.2 and 3.1.1) when I tested this.
> So the question is: is IMyInterface indeed a CORBA interface? If so
> then why does the compiler not complain? If not then why doesn't it
> work? For both cases a full example would be needed to investigate
> this.
>
> Regards,
> Sven
Sorry, I got it wrong. The interface was in fact COM style instead of CORBA.

In case of COM interface, why

var
    aInt : IMyInterface;
   aObj : TObject;
begin
....
....
...
    aInt := MyList.Items[0];

    aObj := aInt as TObject;

why sometimes (not always)   aInt  <>   (aObj as IMyInterface ) ?

Dennis
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Why casting interface as Tobject will result in a different reference?

Free Pascal - General mailing list
2017-05-27 18:10 GMT+02:00 Dennis Poon <[hidden email]>:

>
>
> Sven Barth via fpc-pascal wrote:
>>
>> The idea itself is valid, cause "(IMyInterfaceVar as TObject) as
>> IMyInterface = IMyInterfaceVar" is true if and only if IMyInterface is
>> a COM interface. If IMyInterface really is a CORBA interface as Dennis
>> wrote then the compiler should already have complained at the "aObj :=
>> aInt as TObject" line as this type of conversion is*not*  supported by
>> CORBA interfaces (cause they don't provide QueryInterface() which is
>> needed for this). At least for me the compiler*did*  complain (both
>> 3.0.2 and 3.1.1) when I tested this.
>> So the question is: is IMyInterface indeed a CORBA interface? If so
>> then why does the compiler not complain? If not then why doesn't it
>> work? For both cases a full example would be needed to investigate
>> this.
>>
>> Regards,
>> Sven
>
> Sorry, I got it wrong. The interface was in fact COM style instead of CORBA.
>
> In case of COM interface, why
>
> var
>    aInt : IMyInterface;
>   aObj : TObject;
> begin
> ....
> ....
> ...
>    aInt := MyList.Items[0];
>
>    aObj := aInt as TObject;
>
> why sometimes (not always)   aInt  <>   (aObj as IMyInterface ) ?

Would you please provide a full, yet simple as possible example that
shows this? I can't reproduce it with merely code fragments as for me
it works.

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal