Access violation when unloading modules in Delphi host with .NET plugin

I have a Delphi host using a .NET plugin. When running in debug mode in Delphi I am sometimes getting an access violation when calling the THYModuleManager.UnloadModules method at the point it does the OleCheck in THYClr.UnloadDomain(Domain: _AppDomain). This error is not shown when not running in debug mode so I don’t know if it is something I should be worried about or not, or what the reason for this error is. I assume the error is occurring when trying to unload my plugin dll. Can you suggest anything that I can check in my code as to what may cause this?

You can ignore this for now as I think I may have found some of our code that is causing this problem but I need to investigate further.

I have just started looking into this again and still have an issue in that it happens when we use some Delphi third party software called EurekaLog and enable a feature that catches memory leaks so I don’t know if its something I need to worry about or not. I know it only happens when I make use of an event handler interface I have created so am wondering if I am doing something wrong.

In Visual Studio I have created the following

public interface ITextControlEvents : IHYCrossPlatformInterface
{
void Changed();
}

and also a method of my actual plugin control in Visual Studio

public interface ITXTextControlPlugin : IHYCrossPlatformInterface
{

void RegisterEventHandler(ITextControlEvents textControlEventHandler)

}

In Delphi I have a class that then implements this interface and some code then calls the RegisterEventHandler method passing the object that implements the interface.

The actual events work fine, it is just on closing the app that this error occurs. However, I have noticed it doesn’t occur if the Delphi object that implements the ITextControlEvents is a TForm object which is confusing me, i.e. if I have

TForm1 = class(TForm, ITextControlEvents) and pass the form object to the RegisterEventHandler method it doesn’t generate an error but if I have say

TcdsTXTextControlDotNetEvents = class(TComponent, ITextControlEvents)

and create an object of this class and then pass that to the RegisterEventHandler method it does generate the error.

Any ideas or suggestions?

It seems that your class is not destroyed properly, please try to use THYFakeIDispatch as a base class for your object that implements ITextControlEvents. This class properly supports auto reference count and will be destroyed once refcount reaches zero.

Thanks very much. That appears to have worked.