Use base::ComPtr in the FileSystemModule

This commit is contained in:
David Capello 2021-06-15 18:23:49 -03:00
parent 5f00e6f96a
commit 92c5222aa7
2 changed files with 25 additions and 35 deletions

2
laf

@ -1 +1 @@
Subproject commit 08c0a7bc1405a7e521c5790c72710d229469fa19 Subproject commit 12dea43b35daf92a6087f3a980aff767ac0b7043

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2019-2021 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello // Copyright (C) 2001-2018 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -19,6 +19,7 @@
#include "base/fs.h" #include "base/fs.h"
#include "base/string.h" #include "base/string.h"
#include "base/win/comptr.h"
#include "os/display.h" #include "os/display.h"
#include "os/surface.h" #include "os/surface.h"
#include "os/system.h" #include "os/system.h"
@ -64,8 +65,8 @@ FileItemMap* fileitems_map = nullptr;
unsigned int current_file_system_version = 0; unsigned int current_file_system_version = 0;
#ifdef _WIN32 #ifdef _WIN32
IMalloc* shl_imalloc = nullptr; base::ComPtr<IMalloc> shl_imalloc;
IShellFolder* shl_idesktop = nullptr; base::ComPtr<IShellFolder> shl_idesktop;
#endif #endif
// a position in the file-system // a position in the file-system
@ -213,17 +214,14 @@ FileSystemModule::~FileSystemModule()
fileitems_map->clear(); fileitems_map->clear();
#ifdef _WIN32 #ifdef _WIN32
// relase desktop IShellFolder interface // Release interfaces
shl_idesktop->Release(); shl_idesktop.reset();
shl_imalloc.reset();
// release IMalloc interface
shl_imalloc->Release();
shl_imalloc = NULL;
#endif #endif
delete fileitems_map; delete fileitems_map;
m_instance = NULL; m_instance = nullptr;
} }
FileSystemModule* FileSystemModule::instance() FileSystemModule* FileSystemModule::instance()
@ -437,28 +435,31 @@ const FileItemList& FileItem::children()
//LOG("FS: Loading files for %p (%s)\n", fileitem, fileitem->displayname); //LOG("FS: Loading files for %p (%s)\n", fileitem, fileitem->displayname);
#ifdef _WIN32 #ifdef _WIN32
{ {
IShellFolder* pFolder = NULL; base::ComPtr<IShellFolder> pFolder;
HRESULT hr; HRESULT hr;
if (this == rootitem) if (this == rootitem) {
pFolder = shl_idesktop; pFolder = shl_idesktop;
}
else { else {
hr = shl_idesktop->BindToObject(m_fullpidl, hr = shl_idesktop->BindToObject(
NULL, IID_IShellFolder, (LPVOID *)&pFolder); m_fullpidl, nullptr,
IID_IShellFolder, (LPVOID *)&pFolder);
if (hr != S_OK) if (hr != S_OK)
pFolder = NULL; pFolder = nullptr;
} }
if (pFolder != NULL) { if (pFolder) {
IEnumIDList *pEnum = NULL; base::ComPtr<IEnumIDList> pEnum;
ULONG c, fetched; ULONG c, fetched;
// Get the interface to enumerate subitems // Get the interface to enumerate subitems
hr = pFolder->EnumObjects(reinterpret_cast<HWND>(os::instance()->defaultDisplay()->nativeHandle()), hr = pFolder->EnumObjects(
reinterpret_cast<HWND>(os::instance()->defaultDisplay()->nativeHandle()),
SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &pEnum); SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &pEnum);
if (hr == S_OK && pEnum != NULL) { if (hr == S_OK && pEnum) {
LPITEMIDLIST itempidl[256]; LPITEMIDLIST itempidl[256];
SFGAOF attribs[256]; SFGAOF attribs[256];
@ -468,7 +469,7 @@ const FileItemList& FileItem::children()
// item is file or a folder // item is file or a folder
for (c=0; c<fetched; ++c) { for (c=0; c<fetched; ++c) {
attribs[c] = SFGAO_FOLDER; attribs[c] = SFGAO_FOLDER;
pFolder->GetAttributesOf(1, (LPCITEMIDLIST *)itempidl, attribs+c); pFolder->GetAttributesOf(1, (LPCITEMIDLIST*)itempidl, attribs+c);
} }
// Generate the FileItems // Generate the FileItems
@ -495,12 +496,7 @@ const FileItemList& FileItem::children()
insertChildSorted(child); insertChildSorted(child);
} }
} }
pEnum->Release();
} }
if (pFolder != shl_idesktop)
pFolder->Release();
} }
} }
#else #else
@ -688,7 +684,7 @@ static SFGAOF get_pidl_attrib(FileItem* fileitem, SFGAOF attrib)
HRESULT hr; HRESULT hr;
IShellFolder* pFolder = nullptr; base::ComPtr<IShellFolder> pFolder;
if (fileitem->m_parent == rootitem) if (fileitem->m_parent == rootitem)
pFolder = shl_idesktop; pFolder = shl_idesktop;
else { else {
@ -703,8 +699,6 @@ static SFGAOF get_pidl_attrib(FileItem* fileitem, SFGAOF attrib)
hr = pFolder->GetAttributesOf(1, (LPCITEMIDLIST*)&fileitem->m_pidl, &attrib2); hr = pFolder->GetAttributesOf(1, (LPCITEMIDLIST*)&fileitem->m_pidl, &attrib2);
if (hr == S_OK) if (hr == S_OK)
attrib = attrib2; attrib = attrib2;
if (pFolder && pFolder != shl_idesktop)
pFolder->Release();
} }
return attrib; return attrib;
} }
@ -714,7 +708,7 @@ static void update_by_pidl(FileItem* fileitem, SFGAOF attrib)
{ {
STRRET strret; STRRET strret;
WCHAR pszName[MAX_PATH]; WCHAR pszName[MAX_PATH];
IShellFolder* pFolder = NULL; base::ComPtr<IShellFolder> pFolder;
HRESULT hr; HRESULT hr;
if (fileitem == rootitem) if (fileitem == rootitem)
@ -724,7 +718,7 @@ static void update_by_pidl(FileItem* fileitem, SFGAOF attrib)
hr = shl_idesktop->BindToObject(fileitem->m_parent->m_fullpidl, hr = shl_idesktop->BindToObject(fileitem->m_parent->m_fullpidl,
nullptr, IID_IShellFolder, (LPVOID*)&pFolder); nullptr, IID_IShellFolder, (LPVOID*)&pFolder);
if (hr != S_OK) if (hr != S_OK)
pFolder = NULL; pFolder = nullptr;
} }
// Get the file name // Get the file name
@ -769,10 +763,6 @@ static void update_by_pidl(FileItem* fileitem, SFGAOF attrib)
else { else {
fileitem->m_displayname = base::get_file_name(fileitem->m_filename); fileitem->m_displayname = base::get_file_name(fileitem->m_filename);
} }
if (pFolder && pFolder != shl_idesktop) {
pFolder->Release();
}
} }
static LPITEMIDLIST concat_pidl(LPITEMIDLIST pidlHead, LPITEMIDLIST pidlTail) static LPITEMIDLIST concat_pidl(LPITEMIDLIST pidlHead, LPITEMIDLIST pidlTail)