previewpane: use RAII for FileExplorerPreviewSettings classes to avoid memory leaks

This commit is contained in:
yuyoyuppe 2020-10-20 15:01:45 +03:00 committed by Andrey Nekrasov
parent 6c62be1818
commit f506980e4d
7 changed files with 43 additions and 54 deletions

View File

@ -10,38 +10,36 @@
// Constructor
PowerPreviewModule::PowerPreviewModule() :
m_moduleName(GET_RESOURCE_STRING(IDS_MODULE_NAME)),
app_key(powerpreviewConstants::ModuleKey),
m_fileExplorerModules(
{ // SVG Preview Handler settings object.
new PreviewHandlerSettings(
true,
L"svg-previewer-toggle-setting",
GET_RESOURCE_STRING(IDS_PREVPANE_SVG_SETTINGS_DESCRIPTION),
L"{ddee2b8a-6807-48a6-bb20-2338174ff779}",
L"Svg Preview Handler",
new RegistryWrapper()),
// MarkDown Preview Handler Settings Object.
new PreviewHandlerSettings(
true,
L"md-previewer-toggle-setting",
GET_RESOURCE_STRING(IDS_PREVPANE_MD_SETTINGS_DESCRIPTION),
L"{45769bcc-e8fd-42d0-947e-02beef77a1f5}",
L"Markdown Preview Handler",
new RegistryWrapper()),
//SVG Thumbnail Provider settings object.
new ThumbnailProviderSettings(
true,
L"svg-thumbnail-toggle-setting",
GET_RESOURCE_STRING(IDS_SVG_THUMBNAIL_PROVIDER_SETTINGS_DESCRIPTION),
L"{36B27788-A8BB-4698-A756-DF9F11F64F84}",
L"Svg Thumbnail Provider",
new RegistryWrapper(),
L".svg\\shellex\\{E357FCCD-A995-4576-B01F-234630154E96}") })
app_key(powerpreviewConstants::ModuleKey)
{
// Initialize the toggle states for each module
init_settings();
m_fileExplorerModules.emplace_back(std::make_unique<PreviewHandlerSettings>(
true,
L"svg-previewer-toggle-setting",
GET_RESOURCE_STRING(IDS_PREVPANE_SVG_SETTINGS_DESCRIPTION),
L"{ddee2b8a-6807-48a6-bb20-2338174ff779}",
L"Svg Preview Handler",
std::make_unique<RegistryWrapper>()));
m_fileExplorerModules.emplace_back(std::make_unique<PreviewHandlerSettings>(
true,
L"md-previewer-toggle-setting",
GET_RESOURCE_STRING(IDS_PREVPANE_MD_SETTINGS_DESCRIPTION),
L"{45769bcc-e8fd-42d0-947e-02beef77a1f5}",
L"Markdown Preview Handler",
std::make_unique<RegistryWrapper>()));
m_fileExplorerModules.emplace_back(std::make_unique<ThumbnailProviderSettings>(
true,
L"svg-thumbnail-toggle-setting",
GET_RESOURCE_STRING(IDS_SVG_THUMBNAIL_PROVIDER_SETTINGS_DESCRIPTION),
L"{36B27788-A8BB-4698-A756-DF9F11F64F84}",
L"Svg Thumbnail Provider",
std::make_unique<RegistryWrapper>(),
L".svg\\shellex\\{E357FCCD-A995-4576-B01F-234630154E96}"));
// If the user is on the new settings interface, File Explorer might be disabled if they updated from old to new settings, so initialize the registry state in the constructor as PowerPreviewModule::enable/disable will not be called on startup
if (UseNewSettings())
{
@ -87,7 +85,7 @@ bool PowerPreviewModule::get_config(_Out_ wchar_t* buffer, _Out_ int* buffer_siz
GET_RESOURCE_STRING(IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_DESC),
GET_RESOURCE_STRING(IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_TEXT));
for (auto fileExplorerModule : this->m_fileExplorerModules)
for (auto& fileExplorerModule : m_fileExplorerModules)
{
settings.add_bool_toggle(
fileExplorerModule->GetToggleSettingName(),
@ -107,7 +105,7 @@ void PowerPreviewModule::set_config(const wchar_t* config)
bool updateSuccess = true;
bool isElevated = is_process_elevated(false);
for (auto fileExplorerModule : this->m_fileExplorerModules)
for (auto& fileExplorerModule : m_fileExplorerModules)
{
// If the user is using the new settings interface, as it does not have a toggle to modify enabled consider File Explorer to always be enabled
updateSuccess = updateSuccess && fileExplorerModule->UpdateState(settings, this->m_enabled || UseNewSettings(), isElevated);
@ -147,7 +145,7 @@ void PowerPreviewModule::enable()
void PowerPreviewModule::disable()
{
elevation_check_wrapper([this]() {
for (auto fileExplorerModule : this->m_fileExplorerModules)
for (auto& fileExplorerModule : m_fileExplorerModules)
{
fileExplorerModule->Disable();
}
@ -177,7 +175,7 @@ void PowerPreviewModule::init_settings()
PowerToysSettings::PowerToyValues::load_from_settings_file(PowerPreviewModule::get_key());
// Load settings states.
for (auto fileExplorerModule : this->m_fileExplorerModules)
for (auto& fileExplorerModule : m_fileExplorerModules)
{
fileExplorerModule->LoadState(settings);
}
@ -191,7 +189,7 @@ void PowerPreviewModule::init_settings()
// Function to check if the registry states need to be updated
bool PowerPreviewModule::is_registry_update_required()
{
for (auto fileExplorerModule : this->m_fileExplorerModules)
for (auto& fileExplorerModule : m_fileExplorerModules)
{
if (fileExplorerModule->GetToggleSettingState() != fileExplorerModule->CheckRegistryState())
{
@ -241,7 +239,7 @@ void PowerPreviewModule::elevation_check_wrapper(std::function<void()> method)
void PowerPreviewModule::update_registry_to_match_toggles()
{
registry_and_elevation_check_wrapper([this]() {
for (auto fileExplorerModule : this->m_fileExplorerModules)
for (auto& fileExplorerModule : m_fileExplorerModules)
{
if (fileExplorerModule->GetToggleSettingState())
{

View File

@ -21,7 +21,7 @@ private:
std::wstring m_moduleName;
//contains the non localized key of the powertoy
std::wstring app_key;
std::vector<FileExplorerPreviewSettings*> m_fileExplorerModules;
std::vector<std::unique_ptr<FileExplorerPreviewSettings>> m_fileExplorerModules;
// Function to check if the registry states need to be updated
bool is_registry_update_required();

View File

@ -11,8 +11,8 @@ namespace PowerPreviewSettings
static const LPCWSTR preview_handlers_subkey;
public:
PreviewHandlerSettings(bool toggleSettingEnabled, const std::wstring& toggleSettingName, const std::wstring& toggleSettingDescription, LPCWSTR clsid, const std::wstring& registryValueData, RegistryWrapperIface* registryWrapper) :
FileExplorerPreviewSettings(toggleSettingEnabled, toggleSettingName, toggleSettingDescription, clsid, registryValueData, registryWrapper)
PreviewHandlerSettings(bool toggleSettingEnabled, const std::wstring& toggleSettingName, const std::wstring& toggleSettingDescription, LPCWSTR clsid, const std::wstring& registryValueData, std::unique_ptr<RegistryWrapperIface> registryWrapper) :
FileExplorerPreviewSettings(toggleSettingEnabled, toggleSettingName, toggleSettingDescription, clsid, registryValueData, std::move(registryWrapper))
{
}

View File

@ -12,24 +12,16 @@ namespace PowerPreviewSettings
extern "C" IMAGE_DOS_HEADER __ImageBase;
// Base Settings Class Implementation
FileExplorerPreviewSettings::FileExplorerPreviewSettings(bool toggleSettingEnabled, const std::wstring& toggleSettingName, const std::wstring& toggleSettingDescription, LPCWSTR clsid, const std::wstring& registryValueData, RegistryWrapperIface* registryWrapper) :
FileExplorerPreviewSettings::FileExplorerPreviewSettings(bool toggleSettingEnabled, const std::wstring& toggleSettingName, const std::wstring& toggleSettingDescription, LPCWSTR clsid, const std::wstring& registryValueData, std::unique_ptr<RegistryWrapperIface> registryWrapper) :
m_toggleSettingEnabled(toggleSettingEnabled),
m_toggleSettingName(toggleSettingName),
m_toggleSettingDescription(toggleSettingDescription),
m_clsid(clsid),
m_registryValueData(registryValueData),
m_registryWrapper(registryWrapper)
m_registryWrapper(std::move(registryWrapper))
{
}
FileExplorerPreviewSettings::~FileExplorerPreviewSettings()
{
if (this->m_registryWrapper != NULL)
{
delete this->m_registryWrapper;
}
}
bool FileExplorerPreviewSettings::GetToggleSettingState() const
{
return this->m_toggleSettingEnabled;

View File

@ -18,11 +18,10 @@ namespace PowerPreviewSettings
LPCWSTR m_clsid;
protected:
RegistryWrapperIface* m_registryWrapper;
std::unique_ptr<RegistryWrapperIface> m_registryWrapper;
public:
FileExplorerPreviewSettings(bool toggleSettingEnabled, const std::wstring& toggleSettingName, const std::wstring& toggleSettingDescription, LPCWSTR clsid, const std::wstring& registryValueData, RegistryWrapperIface* registryWrapper);
~FileExplorerPreviewSettings();
FileExplorerPreviewSettings(bool toggleSettingEnabled, const std::wstring& toggleSettingName, const std::wstring& toggleSettingDescription, LPCWSTR clsid, const std::wstring& registryValueData, std::unique_ptr<RegistryWrapperIface>);
virtual bool GetToggleSettingState() const;
virtual void UpdateToggleSettingState(bool state);

View File

@ -11,8 +11,8 @@ namespace PowerPreviewSettings
LPCWSTR thumbnail_provider_subkey;
public:
ThumbnailProviderSettings(bool toggleSettingEnabled, const std::wstring& toggleSettingName, const std::wstring& toggleSettingDescription, LPCWSTR clsid, const std::wstring& registryValueData, RegistryWrapperIface* registryWrapper, LPCWSTR subkey) :
FileExplorerPreviewSettings(toggleSettingEnabled, toggleSettingName, toggleSettingDescription, clsid, registryValueData, registryWrapper), thumbnail_provider_subkey(subkey)
ThumbnailProviderSettings(bool toggleSettingEnabled, const std::wstring& toggleSettingName, const std::wstring& toggleSettingDescription, LPCWSTR clsid, const std::wstring& registryValueData, std::unique_ptr<RegistryWrapperIface> registryWrapper, LPCWSTR subkey) :
FileExplorerPreviewSettings(toggleSettingEnabled, toggleSettingName, toggleSettingDescription, clsid, registryValueData, std::move(registryWrapper)), thumbnail_provider_subkey(subkey)
{
}

View File

@ -417,7 +417,7 @@ namespace FileExplorerPreviewSettingsTest
L"valid-description",
L"valid-guid",
L"valid-handler",
registryMock);
std::unique_ptr<RegistryWrapperIface>(registryMock));
}
ThumbnailProviderSettings GetThumbnailProviderSettingsObject(bool defaultState, RegistryWrapperIface* registryMock)
@ -428,7 +428,7 @@ namespace FileExplorerPreviewSettingsTest
L"valid-description",
L"valid-guid",
L"valid-handler",
registryMock,
std::unique_ptr<RegistryWrapperIface>(registryMock),
L"valid-subkey");
}