I'm still trying to come to grips with using Interfaces. I'm implementing them for the sole purpose of interacting with objects which are instantiated within a DLL. When I use it, everything works fine, all the methods work as expected, etc. The issue is when it comes to cleaning up the object behind that interface.
I have a simple interface like so
IMyInterface = interface
['{d52b14f3-156b-4df8-aa16-cb353193d27c}']
procedure Foo;
end;
And an object for it
TMyObject = class(TInterfacedObject, IMyInterface)
private
procedure Foo;
end;
Inside the DLL I have a global variable of this object as well as two exported functions to create and destroy this instance
var
_MyObject: TMyObject;
function CreateMyObject: IMyInterface; stdcall;
begin
_MyObject:= TMyObject.Create;
Result:= IMyInterface(_MyObject);
end;
function DestroyMyObject: Integer; stdcall;
begin
_MyObject.Free; // <-- Invalid Pointer Operation
end;
The destructor of the object does virtually nothing, just inherited
and I still have this issue. But I get Invalid Pointer Operation
on _MyObject.Free
.
I use LoadLibrary
and GetProcAddress
to access these exported methods.
Why am I getting this and how do I fix it?