Isn’t MyObject2 still pointing to MyObject1?
If you go back to the FPC documentation, in the User Guide it says "Objects are stored in memory just as ordinary records with an extra field: a pointer to the Virtual Method Table (VMT)." My understanding is that an interface is stored similarly, except that a different VMT is used i.e. that which defines the interface. If you try and assign one to the other by just coercing the type then you will get undefined results.


On 11/11/16 11:25, Ryan Joseph wrote:
On Nov 11, 2016, at 5:52 PM, Tony Whyman <tony.why...@mccallumwhyman.com> wrote:

Someone else may correct me, but with CORBA, I believe you have to explicitly 
add a function to the interface such as

function GetObject: TMyObject;

and implement as

function TMyObject.GetObject: TMyObject;
begin
  Result := self;
end;
I think I’m out of time now but I’ll try this in my code that was crashing 
before.

How is this different from just casting the interface to an object? There’s 
your example below basically like I was doing it before with the seemingly 
random crashes. Isn’t MyObject2 still pointing to MyObject1?

type
IMyInterface = interface
    procedure MyProc;
end;

TMyObjectClass = class(TInterfacedObject,IMyInterface)
public
   procedure MyProc;
end;

var MyObject1, MyObject2: TMyObjectClass;
      MyInterface: IMyInterface;

begin
  MyObject1 := TMyObjectClass.Create;
  MyInterface := MyObject1;

  MyInterface.MyProc;

  MyObject2 := TMyObjectClass(MyInterface);

  MyObject1.Free {OK for CORBA - will cause an exception in COM}
  {MyObject1, MyObject2 and MyInterface are now all invalid}
end;


Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to