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/taskbarbutton.h"
#include "wx/scopedptr.h"
#include "wx/msw/private/comptr.h"
#include <shlwapi.h>
#include <initguid.h>
@@ -373,8 +374,8 @@ THUMBBUTTONFLAGS GetNativeThumbButtonFlags(const wxThumbBarButton& button)
bool AddShellLink(IObjectCollection *collection,
const wxTaskBarJumpListItem& item)
{
IShellLink* shellLink = NULL;
IPropertyStore* propertyStore = NULL;
wxCOMPtr<IShellLink> shellLink;
wxCOMPtr<IPropertyStore> 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<IPropertyStore> 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<ICustomDestinationList> m_destinationList;
wxCOMPtr<IObjectArray> m_objectArray;
wxScopedPtr<wxTaskBarJumpListCategory> m_tasks;
wxScopedPtr<wxTaskBarJumpListCategory> m_frequent;
@@ -701,7 +698,7 @@ bool wxThumbBarButton::UpdateParentTaskBarButton()
/* static */
wxTaskBarButton* wxTaskBarButton::New(wxWindow* parent)
{
wxITaskbarList3* taskbarList = NULL;
wxCOMPtr<wxITaskbarList3> 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<void**>(&(m_objectArray)));
wxIID_IObjectArray, reinterpret_cast<void**>(&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<IObjectCollection> 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<IObjectCollection> 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<IApplicationDocumentLists> 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<IObjectArray> 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<IUnknown> collectionItem;
hr = array->GetAt(i, wxIID_IUnknown,
reinterpret_cast<void **>(&collectionItem));
if ( FAILED(hr) )
@@ -1526,21 +1517,19 @@ void wxTaskBarJumpListImpl::LoadKnownCategory(const wxString& title)
continue;
}
IShellLink *shellLink = NULL;
IShellItem *shellItem = NULL;
wxCOMPtr<IShellLink> shellLink;
wxCOMPtr<IShellItem> shellItem;
wxTaskBarJumpListItem* item = NULL;
if ( SUCCEEDED(collectionItem->QueryInterface(
wxIID_IShellLink, reinterpret_cast<void**>(&shellLink))) )
{
item = GetItemFromIShellLink(shellLink);
shellLink->Release();
}
else if ( SUCCEEDED(collectionItem->QueryInterface(
wxIID_IShellItem, reinterpret_cast<void**>(&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