当我需要在C#中通过COM查询的接口上调用Marshal.ReleaseComObjec
我一直在使用一些DirectShow接口来播放使用C#和
DirectShow.Net的数字电视(DVB-T).我最近遇到的一个运行时错误COM对象与其底层的RCW分离不能使用.
此错误发生在以下行中: _guideData = _transportInformationFilter as IGuideData; _transportInformationFilter类型为IBaseFilter,它是先前通过DirectShow.Net实用功能分配的COM对象. 我假设错误是由于_transportInformationFilter以某种方式提前释放,我将其跟踪到以下方法(删除错误处理): private void AttachGuideDataEvent() { IConnectionPoint connPoint = null; IConnectionPointContainer connPointContainer = null; try { connPointContainer = _transportInformationFilter as IConnectionPointContainer; if (connPointContainer == null) /* error */ var guideDataEventGuid = typeof (IGuideDataEvent).GUID; connPointContainer.FindConnectionPoint(ref guideDataEventGuid,out connPoint); if (connPoint == null) /* error */ int cookie; connPoint.Advise(this,out cookie); if (cookie == 0) /* error */ _persistIGuideDataEventCookie = cookie; } finally { if (connPointContainer != null) Marshal.ReleaseComObject(connPointContainer); if (connPoint != null) Marshal.ReleaseComObject(connPoint); } } 据我所知,connPointContainer = _transportInformationFilter,因为IConnectionPointContainer应该导致在_transportInformationFilter COM对象上调用QueryInterface,因此需要单独释放.但是,对Marshal.ReleaseComObject(connPointContainer)的调用是导致_transportInformationFilter与其RCW分离的祸害;删除此调用修复了问题. 鉴于此,在什么情况下,我需要在C#中显式释放COM对象(使用Marshal.ReleaseComObject)以避免资源泄漏? 解决方法
几乎从不. ReleaseComObject管理RCW的引用计数,而不是基础对象,并不直接类似于IUnknown.Release.你应该让CLR管理它的QueryInterface和释放.
…
从http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject.aspx FYI,调用IUnknown.Release的方式直接是 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |