Memory leaks on IUIAutomationEventHandler

  • Thread starter Thread starter mesajflaviu
  • Start date Start date
M

mesajflaviu

Guest
I have made a sample app, SDI, where I run a thread with IUIAutomationEventHandler:

UINT __stdcall UIAMonitor(LPVOID pv);

class CEventHandler : public IUIAutomationEventHandler
{
public:
CEventHandler(IUIAutomation* pAuto, IUIAutomationCacheRequest* pBCache, IUIAutomationCondition* pBCond);
STDMETHOD (QueryInterface)(REFIID riid, void** ppv);
STDMETHOD_(ULONG, AddRef)();
STDMETHOD_(ULONG, Release)();
STDMETHOD(HandleAutomationEvent)(IUIAutomationElement* sender, EVENTID eventId);

private:
~CEventHandler();
LONG m_cRef;
IUIAutomation* m_pAuto;
IUIAutomationCacheRequest* m_pButtonCacheRequest;
IUIAutomationCondition* m_pBCond;
};



and in OnInitialUpdate a start the thread:

void CTTTView::OnInitialUpdate()
{
CHtmlView::OnInitialUpdate();

HANDLE hThread = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, UIAMonitor, &m_hEvent.m_h, 0, nullptr));
if (hThread)
{
CloseHandle(hThread);
}
else
{
MessageBox(_T("Failed to create UIAMonitor thread"), NULL, MB_ICONERROR);
}

SetSilent(TRUE);
Navigate2(_T("https://www.codeproject.com"));
}

and here is the UIAMonitor function:

UINT __stdcall UIAMonitor(LPVOID pv)
{
HRESULT hres, hr = E_FAIL;
HANDLE hEvent = *(static_cast<PHANDLE>(pv));
if (SUCCEEDED(hres = CoInitializeEx(NULL, COINIT_MULTITHREADED)))
{
CComPtr<IUIAutomation> pAutomation;
hr = CoCreateInstance(CLSID_CUIAutomation, NULL, CLSCTX_INPROC_SERVER, IID_IUIAutomation, reinterpret_cast<LPVOID*>(&pAutomation));
if (pAutomation)
{
CComPtr<IUIAutomationElement> pRoot;
CComPtr<IUIAutomationCacheRequest> pButtonCacheRequest;
CComPtr<IUIAutomationCondition> pCondButton;
CComPtr<IUIAutomationEventHandler> pHandler;
CComVariant vButton(UIA_ButtonControlTypeId);
hr = pAutomation->CreateCacheRequest(&pButtonCacheRequest);
if (pButtonCacheRequest)
{
hr = pButtonCacheRequest->AddProperty(UIA_NamePropertyId);
hr = pButtonCacheRequest->AddProperty(UIA_AutomationIdPropertyId);
hr = pButtonCacheRequest->AddPattern(UIA_InvokePatternId);
}
hr = pAutomation->CreatePropertyCondition(UIA_ControlTypePropertyId, vButton, &pCondButton);
if (pCondButton && pButtonCacheRequest)
{
CEventHandler* pEventObject = new CEventHandler(pAutomation, pButtonCacheRequest, pCondButton); // <--- memory leaks
if (pEventObject)
{
hr = pEventObject->QueryInterface(IID_PPV_ARGS(&pHandler));
pEventObject->Release();
}
if (pHandler)
{
hr = pAutomation->GetRootElement(&pRoot); // Receive window events for all new windows created under the desktop

if (pRoot)
{
hr = pAutomation->AddAutomationEventHandler(UIA_Window_WindowOpenedEventId,
pRoot, TreeScope_Descendants, NULL, pHandler);
}
}
}
DWORD dwWake = WaitForSingleObject(hEvent, INFINITE); // Keeps event handler 7 seconds /*alive until program terminates*/
ATLASSERT(dwWake == WAIT_OBJECT_0);
hr = pAutomation->RemoveAllEventHandlers();
}
}

if (SUCCEEDED(hres))
CoUninitialize();

return hr;
}


the problem is that sometime, not all the time, at application exit, I got memory leaks:

Dumping objects ->
d:\tempx\ttt\uiaeventmonitor.cpp(34) : {25387} normal block at 0x059D9528, 20 bytes long.
Data: < ( z > 0C DA 28 01 02 00 00 00 D8 98 EA 00 98 7A EB 00
Object dump complete.


And here is where I setup, ans stop the thread:

CTTTView::CTTTView()
{
// TODO: add construction code here

m_hEvent.Attach(CreateEvent(NULL, FALSE, FALSE, NULL)); // Automatic reset, not signalled
}

void CTTTView::OnDestroy()
{
SetEvent(m_hEvent); // signal UIAMonitor thread to exit

CHtmlView::OnDestroy();

// TODO: Add your message handler code here
}


I say again: sometime I got ML, sometime not ... which indicate that something is not right ... I attach a sample project to illustrate this behavior ... can you tell me what I have done wrong here ? Thank you.

Continue reading...
 
Back
Top