2
由於RefCount在AtlUnadvise()後沒有減少,我們發現內存泄漏問題。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對象沒有得到適當的釋放。
在這些課堂中是否有我錯過的東西?
任何參考將不勝感激,在此先感謝。
http://resources.esri.com/help/9.3/arcgisdesktop/com/com/vcpp/events.htm - 請參閱標題**事件通知參考問題** – Flot2011 2012-04-27 06:30:58
呃,您究竟看到託管界面的CCW引用計數?這被深埋在CLR中。 – 2012-04-27 17:36:16