From f7458dd03f29d3280006b8875a156b7b09be7257 Mon Sep 17 00:00:00 2001 From: Tobias Taschner Date: Thu, 17 Sep 2015 13:45:39 +0200 Subject: [PATCH] Use wxCOMPtr<> instead of raw pointers in wxTaskBarButton code Simplify the code and ensure its safety by using smart pointers instead of raw Release() calls. Closes #16557. --- src/msw/taskbarbutton.cpp | 47 +++++++++++++-------------------------- 1 file changed, 16 insertions(+), 31 deletions(-) diff --git a/src/msw/taskbarbutton.cpp b/src/msw/taskbarbutton.cpp index f2e04aefd0..577e58315f 100644 --- a/src/msw/taskbarbutton.cpp +++ b/src/msw/taskbarbutton.cpp @@ -28,6 +28,7 @@ #include "wx/msw/private.h" #include "wx/msw/taskbarbutton.h" #include "wx/scopedptr.h" +#include "wx/msw/private/comptr.h" #include #include @@ -373,8 +374,8 @@ THUMBBUTTONFLAGS GetNativeThumbButtonFlags(const wxThumbBarButton& button) bool AddShellLink(IObjectCollection *collection, const wxTaskBarJumpListItem& item) { - IShellLink* shellLink = NULL; - IPropertyStore* propertyStore = NULL; + wxCOMPtr shellLink; + wxCOMPtr propertyStore; HRESULT hr = CoCreateInstance ( @@ -411,7 +412,6 @@ bool AddShellLink(IObjectCollection *collection, if ( FAILED(hr) ) { wxLogApiError("IShellLink(QueryInterface)", hr); - shellLink->Release(); return false; } @@ -437,13 +437,11 @@ bool AddShellLink(IObjectCollection *collection, // Save the changes we made to the property store. propertyStore->Commit(); - propertyStore->Release(); PropVariantClear(&pv); // Add this IShellLink object to the given collection. hr = collection->AddObject(shellLink); - shellLink->Release(); return SUCCEEDED(hr); } @@ -455,7 +453,7 @@ wxTaskBarJumpListItem* GetItemFromIShellLink(IShellLink* link) wxTaskBarJumpListItem* item = new wxTaskBarJumpListItem(NULL, wxTASKBAR_JUMP_LIST_DESTIONATION); - IPropertyStore *linkProps; + wxCOMPtr linkProps; HRESULT hr = link->QueryInterface ( wxIID_IPropertyStore, @@ -471,7 +469,6 @@ wxTaskBarJumpListItem* GetItemFromIShellLink(IShellLink* link) linkProps->GetValue(PKEY_Link_Arguments, &var); item->SetArguments(wxString(var.pwszVal)); PropVariantClear(&var); - linkProps->Release(); const int bufferSize = 2048; wchar_t buffer[bufferSize]; @@ -581,8 +578,8 @@ private: wxTaskBarJumpList *m_jumpList; - ICustomDestinationList *m_destinationList; - IObjectArray *m_objectArray; + wxCOMPtr m_destinationList; + wxCOMPtr m_objectArray; wxScopedPtr m_tasks; wxScopedPtr m_frequent; @@ -701,7 +698,7 @@ bool wxThumbBarButton::UpdateParentTaskBarButton() /* static */ wxTaskBarButton* wxTaskBarButton::New(wxWindow* parent) { - wxITaskbarList3* taskbarList = NULL; + wxCOMPtr taskbarList; HRESULT hr = CoCreateInstance ( @@ -723,7 +720,6 @@ wxTaskBarButton* wxTaskBarButton::New(wxWindow* parent) // This is however unexpected. wxLogApiError(wxT("ITaskbarList3::Init"), hr); - taskbarList->Release(); return NULL; } @@ -1300,9 +1296,6 @@ wxTaskBarJumpListImpl::wxTaskBarJumpListImpl(wxTaskBarJumpList *jumpList, wxTaskBarJumpListImpl::~wxTaskBarJumpListImpl() { - if ( m_destinationList ) - m_destinationList->Release(); - for ( wxTaskBarJumpListCategories::iterator it = m_customCategories.begin(); it != m_customCategories.end(); ++it ) @@ -1416,8 +1409,9 @@ bool wxTaskBarJumpListImpl::BeginUpdate() return false; unsigned int max_count = 0; + m_objectArray = NULL; HRESULT hr = m_destinationList->BeginList(&max_count, - wxIID_IObjectArray, reinterpret_cast(&(m_objectArray))); + wxIID_IObjectArray, reinterpret_cast(&m_objectArray)); if ( !m_appID.empty() ) m_destinationList->SetAppID(m_appID.wc_str()); @@ -1426,7 +1420,6 @@ bool wxTaskBarJumpListImpl::BeginUpdate() bool wxTaskBarJumpListImpl::CommitUpdate() { - m_objectArray->Release(); return SUCCEEDED(m_destinationList->CommitList()); } @@ -1435,7 +1428,7 @@ void wxTaskBarJumpListImpl::AddTasksToDestinationList() if ( !m_tasks.get() ) return; - IObjectCollection* collection = CreateObjectCollection(); + wxCOMPtr collection(CreateObjectCollection()); if ( !collection ) return; @@ -1450,7 +1443,6 @@ void wxTaskBarJumpListImpl::AddTasksToDestinationList() AddShellLink(collection, *(*it)); } m_destinationList->AddUserTasks(collection); - collection->Release(); } void wxTaskBarJumpListImpl::AddCustomCategoriesToDestionationList() @@ -1459,7 +1451,7 @@ void wxTaskBarJumpListImpl::AddCustomCategoriesToDestionationList() it != m_customCategories.end(); ++it ) { - IObjectCollection* collection = CreateObjectCollection(); + wxCOMPtr collection(CreateObjectCollection()); if ( !collection ) continue; @@ -1475,13 +1467,12 @@ void wxTaskBarJumpListImpl::AddCustomCategoriesToDestionationList() } m_destinationList->AppendCategory((*it)->GetTitle().wc_str(), collection); - collection->Release(); } } void wxTaskBarJumpListImpl::LoadKnownCategory(const wxString& title) { - IApplicationDocumentLists *docList = 0; + wxCOMPtr docList; HRESULT hr = CoCreateInstance ( wxCLSID_ApplicationDocumentLists, @@ -1498,7 +1489,7 @@ void wxTaskBarJumpListImpl::LoadKnownCategory(const wxString& title) if ( !m_appID.empty() ) docList->SetAppID(m_appID.wc_str()); - IObjectArray *array = NULL; + wxCOMPtr array; wxASSERT_MSG( title == "Recent" || title == "Frequent", "Invalid title." ); hr = docList->GetList ( @@ -1517,7 +1508,7 @@ void wxTaskBarJumpListImpl::LoadKnownCategory(const wxString& title) array->GetCount(&count); for (UINT i = 0; i < count; ++i) { - IUnknown *collectionItem = NULL; + wxCOMPtr collectionItem; hr = array->GetAt(i, wxIID_IUnknown, reinterpret_cast(&collectionItem)); if ( FAILED(hr) ) @@ -1526,21 +1517,19 @@ void wxTaskBarJumpListImpl::LoadKnownCategory(const wxString& title) continue; } - IShellLink *shellLink = NULL; - IShellItem *shellItem = NULL; + wxCOMPtr shellLink; + wxCOMPtr shellItem; wxTaskBarJumpListItem* item = NULL; if ( SUCCEEDED(collectionItem->QueryInterface( wxIID_IShellLink, reinterpret_cast(&shellLink))) ) { item = GetItemFromIShellLink(shellLink); - shellLink->Release(); } else if ( SUCCEEDED(collectionItem->QueryInterface( wxIID_IShellItem, reinterpret_cast(&shellItem))) ) { item = GetItemFromIShellItem(shellItem); - shellItem->Release(); } else { @@ -1554,11 +1543,7 @@ void wxTaskBarJumpListImpl::LoadKnownCategory(const wxString& title) else m_recent->Append(item); } - collectionItem->Release(); } - - array->Release(); - docList->Release(); } #endif // wxUSE_TASKBARBUTTON