I think your main problem relates to what a Delphi object reference is. Consider the following artificial example:
type
TMyRecord = record
a, b, c: Integer;
end;
TMyClass = class
a, b, c: Integer;
end;
...
var
MyRecord: TMyRecord;
MyObject: TMyObject;
...
MyObject := TMyObject.Create;
At this point we have instances of two, essentially identical, structured types. However, the way Delphi presents these instances is quite different. The record is what is known as a value type and the object is a reference type.
When you assign to a variable of a reference type, the value is copied. For example:
MyRecord2.a := 1;
MyRecord1 := MyRecord2;
MyRecord1.a := 2;
Assert(MyRecord1.a<>MyRecord2.a);
Examples of value types include integers, enumerated types, floating point types, records, objects etc.
In contrast, assigning to a reference type variable copies the reference so that both variables refer to the same object. For example:
MyObject2.a := 1;
MyObject1 := MyObject2;
MyObject1.a := 2;
Assert(MyObject1.a=MyObject2.a);
One feature of reference types is that they are all heap allocated, whereas value types can be either heap or stack allocated.
Examples of reference types include classes, interfaces, dynamic arrays.
Delphi strings are a funny hybrid. Although they are implemented as references, copy-on-write makes them behave like value types.
Delphi's syntax for objects hides the fact that they are implemented as a reference, i.e. a pointer.
Assert(SizeOf(MyRecord)=3*SizeOf(Integer));//=12
Assert(SizeOf(MyObject)=SizeOf(Pointer));//=4 (or 8 in Delphi 64)
What all this means is that your code is needlessly complicated. There is no need for you to allocate storage for an object reference since an object variable is already a reference. You can write it like this:
//assign object reference to Variant
MyVariant := NativeInt(MyObject);
//extract object reference from Variant
NativeInt(MyObject) := MyVariant;
Note that I am using NativeInt
since it is an integer type that is defined to be at least the same size as a pointer. This will become relevant when the 64 bit version of Delphi appears.