When is the dispose called on RemObjects.Hydra.VisualPlugin (Mixed mode .NET with Delphi XE2)

Even though I am releasing plugin instances via method 3 (below), it seems that the “dispose” of the created plugins does not get called, or at least is only called at some pseudo random time.

From testing of my application, the dispose only definitively gets called when the UnloadModules is called on the ModuleManager.

Testing the mixed mode RemObjects sample shows slightly different behaviour.

When the active plugin is changed via the double click (above) and the current plugin is set to nil, the dispose of the .NET plugin does not directly get called. It appears to be randomly called at some point later, perhaps as the framework is clearing up resources ?

This has come to light when firing events back from a DA server to Hydra clients within a Delphi shell. When the hydra plugin client forms are closed, the events are still finding their way to the closed forms - even though up until that point I had though they had been disposed of with the ModuleManager.ReleaseInstance from Delphi.

Can you confirm :-

1). If this is not the expected behaviour then what can I check / do to ensure that dispose is being triggered when the ReleaseInstance is called from Delphi.
2). If it is the expected behaviour, then what is the suggested way of handling events coming back from a RO/DA server into closed / dead hydra client forms (I’ll work on checking a custom flag set when the plugin is closed and ignoring the events accordingly for now).

Thanks,

Paul.

Hydra itself does not directly control when object is destroyed. When you set instance to nil (which basically means decreasing its reference count) its up to objects implementation on how to react on this. In case of .NET object that was used as a plugin it is still a subject of garbage collection and so .NET GC decides when its actually destroyed.

What you can do is to create a custom interface that you can call to notify your plugin that it should stop receiving events, something like:

[Guid("F64FCFF0-6198-47D6-A8C8-AB5D72F14843")]
public interface ICustomIterface: IHYCrossPlatformInterface
{
    void BeforeClose();
}

implement this interface in your plugin and when you need to close it from the host side you can call:

var
  Obj: ICustomIterface;

if Supports(Instance, ICustomIterface, Obj) then begin
  Obj.BeforeClose;
  Obj := nil;
end

Instance := nil;

You can read more about custom interfaces here

Thanks ejay,

That’s more or less what I ended up doing after posting the question. I was initially surprised to see events being picked up in the hydra plugins that had been previously closed / released, but after I thought about it for a while it made sense as to why it was happening. Your answer confirms the approach I ended up taking.

Thanks Again,

Paul.