开发者

Releasing .NET objects from VB6 code

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜