ATL exe won't exit if DispEventAdvise is used
Hi,
I have an out of process ATL exe hosting a control using
CAxWindow2.CreateControl. An event sink is created and a connection to the
control is created using DispEventAdvise.
The vents are received fine, however the exe will not exit once
DispEventUnadvise is called and all references are released.
If I take out the DispEventAdvise call, the exe will stop correctly after
all references are released.
What am I missing here?
Thanks!
Here is the code that creates the control:
STDMETHODIMP COOPSideUserControl::CreateViewControl(LONG hWnd)
{
try
{
RECT rect;
GetClientRect((HWND) hWnd, &rect);
RECT rect2;
rect2 = rect;
// ATL class to host the control
CAxWindow2 axwnd;
// Create a child window.
// AtlAx functions will subclass this window.
// type of CWindowImpl<CControlContainerWindow>
m_pwndControlContainerWindow = new CControlContainerWindow();
m_pwndControlContainerWindow->Create((HWND) hWnd, rect2, NULL, WS_CHILD |
WS_VISIBLE | WS_BORDER, 0, 1);
// Attach the child window to the CAxWindow so we can access the
// host that subclasses the child window.
axwnd.Attach(m_pwndControlContainerWindow->m_hWnd);
if (axwnd.m_hWnd != NULL)
{
HRESULT hr = axwnd.CreateControl(CONTROLID_MyOOPSideControl);
if (SUCCEEDED(hr))
{
// declared as: CommonInterface::ICommonInterfacePtr m_spOOPSideControl;
HRESULT hr = axwnd.QueryControl(&m_spOOPSideControl);
if (SUCCEEDED(hr))
{
// declared as IUnknown* m_pInnerOOPSideControl;
m_pInnerOOPSideControl = m_spOOPSideControl.GetInterfacePtr();
// Sink events form the control
// If I remove this line, this ATL exe will exit correctly
hr = DispEventAdvise(m_spOOPSideControl,
&__uuidof(CommonInterface::CommonInterfaceEvents));
}
}
}
axwnd.Detach();
}
catch (...)
{
ATLTRACE(TEXT("OOPSideControl::CreateViewControl Erros \n"));
return E_FAIL;
}
return S_OK;
}
STDMETHODIMP COOPSideUserControl::DestroyViewControl(void)
{
ATLTRACE("COOPSideUserControl::DestroyViewControl\n");
// Release the interface
HRESULT hr ;
// Unsink events form the control
hr = DispEventUnadvise(m_spOOPSideControl);
m_spOOPSideControl.Release();
return S_OK;
}