开发者

Releasing .NET objects from VB6 code

开发者 https://www.devze.com 2023-01-03 19:39 出处:网络
On .NET Rocks! Show 561, Carl and Richard talked 开发者_开发知识库about releasing unmanaged objects instantiated in managed code.If you have to release a COM object that\'s instantiated in managed .NE

On .NET Rocks! Show 561, Carl and Richard talked 开发者_开发知识库about releasing unmanaged objects instantiated in managed code. If you have to release a COM object that's instantiated in managed .NET code, you have to call System.Runtime.InteropServices.Marshall.ReleaseComObject. Is there anything similar you have to do (or should do) when releasing .NET objects from COM code, or is it sufficient to rely on the Garbage Collector to release the objects?


As long as you manage the ref count of the COM Callable Wrapper like you do any other COM object (set netObj = Nothing) COM and .NET will take care of the rest.


I would also add that if you use events from VB6, you will want to add a function in your DotNet code to release the Event. E.g.:

class SomeEventClass
{
    public event EventHandler SomeEvent;

    public void DoSomething()
    {
        var someEvent = SomeEvent;
        if (someEvent != null)
        {
             someEvent(this, new EventHandlerArgs());
        }
    }

    public void ReleaseFromEvents()
    {
         SomeEvent = null;
    }

}

This is necessary as sometimes the event will not be cleared to null when the VB6 object is destroyed. Something learned the hard way...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号