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.
This commit is contained in:
Tobias Taschner
2015-09-17 13:45:39 +02:00
committed by Vadim Zeitlin
parent 07380ba0b5
commit f7458dd03f

View File

@@ -28,6 +28,7 @@
#include "wx/msw/private.h" #include "wx/msw/private.h"
#include "wx/msw/taskbarbutton.h" #include "wx/msw/taskbarbutton.h"
#include "wx/scopedptr.h" #include "wx/scopedptr.h"
#include "wx/msw/private/comptr.h"
#include <shlwapi.h> #include <shlwapi.h>
#include <initguid.h> #include <initguid.h>
@@ -373,8 +374,8 @@ THUMBBUTTONFLAGS GetNativeThumbButtonFlags(const wxThumbBarButton& button)
bool AddShellLink(IObjectCollection *collection, bool AddShellLink(IObjectCollection *collection,
const wxTaskBarJumpListItem& item) const wxTaskBarJumpListItem& item)
{ {
IShellLink* shellLink = NULL; wxCOMPtr<IShellLink> shellLink;
IPropertyStore* propertyStore = NULL; wxCOMPtr<IPropertyStore> propertyStore;
HRESULT hr = CoCreateInstance HRESULT hr = CoCreateInstance
( (
@@ -411,7 +412,6 @@ bool AddShellLink(IObjectCollection *collection,
if ( FAILED(hr) ) if ( FAILED(hr) )
{ {
wxLogApiError("IShellLink(QueryInterface)", hr); wxLogApiError("IShellLink(QueryInterface)", hr);
shellLink->Release();
return false; return false;
} }
@@ -437,13 +437,11 @@ bool AddShellLink(IObjectCollection *collection,
// Save the changes we made to the property store. // Save the changes we made to the property store.
propertyStore->Commit(); propertyStore->Commit();
propertyStore->Release();
PropVariantClear(&pv); PropVariantClear(&pv);
// Add this IShellLink object to the given collection. // Add this IShellLink object to the given collection.
hr = collection->AddObject(shellLink); hr = collection->AddObject(shellLink);
shellLink->Release();
return SUCCEEDED(hr); return SUCCEEDED(hr);
} }
@@ -455,7 +453,7 @@ wxTaskBarJumpListItem* GetItemFromIShellLink(IShellLink* link)
wxTaskBarJumpListItem* item = wxTaskBarJumpListItem* item =
new wxTaskBarJumpListItem(NULL, wxTASKBAR_JUMP_LIST_DESTIONATION); new wxTaskBarJumpListItem(NULL, wxTASKBAR_JUMP_LIST_DESTIONATION);
IPropertyStore *linkProps; wxCOMPtr<IPropertyStore> linkProps;
HRESULT hr = link->QueryInterface HRESULT hr = link->QueryInterface
( (
wxIID_IPropertyStore, wxIID_IPropertyStore,
@@ -471,7 +469,6 @@ wxTaskBarJumpListItem* GetItemFromIShellLink(IShellLink* link)
linkProps->GetValue(PKEY_Link_Arguments, &var); linkProps->GetValue(PKEY_Link_Arguments, &var);
item->SetArguments(wxString(var.pwszVal)); item->SetArguments(wxString(var.pwszVal));
PropVariantClear(&var); PropVariantClear(&var);
linkProps->Release();
const int bufferSize = 2048; const int bufferSize = 2048;
wchar_t buffer[bufferSize]; wchar_t buffer[bufferSize];
@@ -581,8 +578,8 @@ private:
wxTaskBarJumpList *m_jumpList; wxTaskBarJumpList *m_jumpList;
ICustomDestinationList *m_destinationList; wxCOMPtr<ICustomDestinationList> m_destinationList;
IObjectArray *m_objectArray; wxCOMPtr<IObjectArray> m_objectArray;
wxScopedPtr<wxTaskBarJumpListCategory> m_tasks; wxScopedPtr<wxTaskBarJumpListCategory> m_tasks;
wxScopedPtr<wxTaskBarJumpListCategory> m_frequent; wxScopedPtr<wxTaskBarJumpListCategory> m_frequent;
@@ -701,7 +698,7 @@ bool wxThumbBarButton::UpdateParentTaskBarButton()
/* static */ /* static */
wxTaskBarButton* wxTaskBarButton::New(wxWindow* parent) wxTaskBarButton* wxTaskBarButton::New(wxWindow* parent)
{ {
wxITaskbarList3* taskbarList = NULL; wxCOMPtr<wxITaskbarList3> taskbarList;
HRESULT hr = CoCreateInstance HRESULT hr = CoCreateInstance
( (
@@ -723,7 +720,6 @@ wxTaskBarButton* wxTaskBarButton::New(wxWindow* parent)
// This is however unexpected. // This is however unexpected.
wxLogApiError(wxT("ITaskbarList3::Init"), hr); wxLogApiError(wxT("ITaskbarList3::Init"), hr);
taskbarList->Release();
return NULL; return NULL;
} }
@@ -1300,9 +1296,6 @@ wxTaskBarJumpListImpl::wxTaskBarJumpListImpl(wxTaskBarJumpList *jumpList,
wxTaskBarJumpListImpl::~wxTaskBarJumpListImpl() wxTaskBarJumpListImpl::~wxTaskBarJumpListImpl()
{ {
if ( m_destinationList )
m_destinationList->Release();
for ( wxTaskBarJumpListCategories::iterator it = m_customCategories.begin(); for ( wxTaskBarJumpListCategories::iterator it = m_customCategories.begin();
it != m_customCategories.end(); it != m_customCategories.end();
++it ) ++it )
@@ -1416,8 +1409,9 @@ bool wxTaskBarJumpListImpl::BeginUpdate()
return false; return false;
unsigned int max_count = 0; unsigned int max_count = 0;
m_objectArray = NULL;
HRESULT hr = m_destinationList->BeginList(&max_count, HRESULT hr = m_destinationList->BeginList(&max_count,
wxIID_IObjectArray, reinterpret_cast<void**>(&(m_objectArray))); wxIID_IObjectArray, reinterpret_cast<void**>(&m_objectArray));
if ( !m_appID.empty() ) if ( !m_appID.empty() )
m_destinationList->SetAppID(m_appID.wc_str()); m_destinationList->SetAppID(m_appID.wc_str());
@@ -1426,7 +1420,6 @@ bool wxTaskBarJumpListImpl::BeginUpdate()
bool wxTaskBarJumpListImpl::CommitUpdate() bool wxTaskBarJumpListImpl::CommitUpdate()
{ {
m_objectArray->Release();
return SUCCEEDED(m_destinationList->CommitList()); return SUCCEEDED(m_destinationList->CommitList());
} }
@@ -1435,7 +1428,7 @@ void wxTaskBarJumpListImpl::AddTasksToDestinationList()
if ( !m_tasks.get() ) if ( !m_tasks.get() )
return; return;
IObjectCollection* collection = CreateObjectCollection(); wxCOMPtr<IObjectCollection> collection(CreateObjectCollection());
if ( !collection ) if ( !collection )
return; return;
@@ -1450,7 +1443,6 @@ void wxTaskBarJumpListImpl::AddTasksToDestinationList()
AddShellLink(collection, *(*it)); AddShellLink(collection, *(*it));
} }
m_destinationList->AddUserTasks(collection); m_destinationList->AddUserTasks(collection);
collection->Release();
} }
void wxTaskBarJumpListImpl::AddCustomCategoriesToDestionationList() void wxTaskBarJumpListImpl::AddCustomCategoriesToDestionationList()
@@ -1459,7 +1451,7 @@ void wxTaskBarJumpListImpl::AddCustomCategoriesToDestionationList()
it != m_customCategories.end(); it != m_customCategories.end();
++it ) ++it )
{ {
IObjectCollection* collection = CreateObjectCollection(); wxCOMPtr<IObjectCollection> collection(CreateObjectCollection());
if ( !collection ) if ( !collection )
continue; continue;
@@ -1475,13 +1467,12 @@ void wxTaskBarJumpListImpl::AddCustomCategoriesToDestionationList()
} }
m_destinationList->AppendCategory((*it)->GetTitle().wc_str(), m_destinationList->AppendCategory((*it)->GetTitle().wc_str(),
collection); collection);
collection->Release();
} }
} }
void wxTaskBarJumpListImpl::LoadKnownCategory(const wxString& title) void wxTaskBarJumpListImpl::LoadKnownCategory(const wxString& title)
{ {
IApplicationDocumentLists *docList = 0; wxCOMPtr<IApplicationDocumentLists> docList;
HRESULT hr = CoCreateInstance HRESULT hr = CoCreateInstance
( (
wxCLSID_ApplicationDocumentLists, wxCLSID_ApplicationDocumentLists,
@@ -1498,7 +1489,7 @@ void wxTaskBarJumpListImpl::LoadKnownCategory(const wxString& title)
if ( !m_appID.empty() ) if ( !m_appID.empty() )
docList->SetAppID(m_appID.wc_str()); docList->SetAppID(m_appID.wc_str());
IObjectArray *array = NULL; wxCOMPtr<IObjectArray> array;
wxASSERT_MSG( title == "Recent" || title == "Frequent", "Invalid title." ); wxASSERT_MSG( title == "Recent" || title == "Frequent", "Invalid title." );
hr = docList->GetList hr = docList->GetList
( (
@@ -1517,7 +1508,7 @@ void wxTaskBarJumpListImpl::LoadKnownCategory(const wxString& title)
array->GetCount(&count); array->GetCount(&count);
for (UINT i = 0; i < count; ++i) for (UINT i = 0; i < count; ++i)
{ {
IUnknown *collectionItem = NULL; wxCOMPtr<IUnknown> collectionItem;
hr = array->GetAt(i, wxIID_IUnknown, hr = array->GetAt(i, wxIID_IUnknown,
reinterpret_cast<void **>(&collectionItem)); reinterpret_cast<void **>(&collectionItem));
if ( FAILED(hr) ) if ( FAILED(hr) )
@@ -1526,21 +1517,19 @@ void wxTaskBarJumpListImpl::LoadKnownCategory(const wxString& title)
continue; continue;
} }
IShellLink *shellLink = NULL; wxCOMPtr<IShellLink> shellLink;
IShellItem *shellItem = NULL; wxCOMPtr<IShellItem> shellItem;
wxTaskBarJumpListItem* item = NULL; wxTaskBarJumpListItem* item = NULL;
if ( SUCCEEDED(collectionItem->QueryInterface( if ( SUCCEEDED(collectionItem->QueryInterface(
wxIID_IShellLink, reinterpret_cast<void**>(&shellLink))) ) wxIID_IShellLink, reinterpret_cast<void**>(&shellLink))) )
{ {
item = GetItemFromIShellLink(shellLink); item = GetItemFromIShellLink(shellLink);
shellLink->Release();
} }
else if ( SUCCEEDED(collectionItem->QueryInterface( else if ( SUCCEEDED(collectionItem->QueryInterface(
wxIID_IShellItem, reinterpret_cast<void**>(&shellItem))) ) wxIID_IShellItem, reinterpret_cast<void**>(&shellItem))) )
{ {
item = GetItemFromIShellItem(shellItem); item = GetItemFromIShellItem(shellItem);
shellItem->Release();
} }
else else
{ {
@@ -1554,11 +1543,7 @@ void wxTaskBarJumpListImpl::LoadKnownCategory(const wxString& title)
else else
m_recent->Append(item); m_recent->Append(item);
} }
collectionItem->Release();
} }
array->Release();
docList->Release();
} }
#endif // wxUSE_TASKBARBUTTON #endif // wxUSE_TASKBARBUTTON