【发布时间】:2012-04-27 04:37:32
【问题描述】:
我们发现内存泄漏问题是由于在 AtlUnadvise() 之后 RefCount 没有递减。
这里是测试代码:
void CCppTesterDlg::OnBnClickedTextbtn()
{
CComObject<CManagedGuiEventSinkImpl> *pEventSink;
TestForm::ITestFormPtr pTestFormPtr( __uuidof(TestForm::TestForm) );
//Attach our COM event sink to the managed dialog to listen for keyboard and mouse events
if( CComObject<CManagedGuiEventSinkImpl>::CreateInstance(&pEventSink) == S_OK )
{
HRESULT hr = S_FALSE;
DWORD dwCookie = 0;
CComPtr<ManagedGuiEventInterface::IManagedGuiEventSink> pSink( pEventSink ); // ref count == 1
hr = AtlAdvise( pTestFormPtr, pSink, __uuidof(ManagedGuiEventInterface::IManagedGuiEventSink), &dwCookie );
// ref count == 2
//pTestFormPtr->OpenDialog();
//Detach the event sink after the modal dialog has been closed
hr = AtlUnadvise( pTestFormPtr, __uuidof(ManagedGuiEventInterface::IManagedGuiEventSink), dwCookie );
// ref count == 2
}
}
CManagedGuiEventSinkImpl 类用于从 C# 模块中获取事件,到目前为止它可以工作,但是我们发现上面的 pEventSink 对象没有正确释放。
我在这些课程中有什么遗漏的吗?
任何参考将不胜感激,在此先感谢。
【问题讨论】:
-
resources.esri.com/help/9.3/arcgisdesktop/com/com/vcpp/… - 请参阅标题下的 Event Circular Reference Issues
-
Erm,您如何查看托管接口的 CCW 上的引用计数?这深埋在 CLR 中。