diff options
author | Rachel Powers <508861+Ryex@users.noreply.github.com> | 2023-05-27 23:22:40 -0700 |
---|---|---|
committer | Rachel Powers <508861+Ryex@users.noreply.github.com> | 2023-05-27 23:22:40 -0700 |
commit | 37420405c7b5dddb003533e1487ba45a2da5b808 (patch) | |
tree | accbc2c677e9f4c2d53757ed44532834d2b9da7b /launcher/ui/pages/instance/ManagedPackPage.cpp | |
parent | c81cb59b4b76bc4558a857c7e13c50629c6b27db (diff) | |
download | PrismLauncher-37420405c7b5dddb003533e1487ba45a2da5b808.tar.gz PrismLauncher-37420405c7b5dddb003533e1487ba45a2da5b808.tar.bz2 PrismLauncher-37420405c7b5dddb003533e1487ba45a2da5b808.zip |
fix(memory leak): refactor NoBigComboStyle -> singleton
Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
Diffstat (limited to 'launcher/ui/pages/instance/ManagedPackPage.cpp')
-rw-r--r-- | launcher/ui/pages/instance/ManagedPackPage.cpp | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/launcher/ui/pages/instance/ManagedPackPage.cpp b/launcher/ui/pages/instance/ManagedPackPage.cpp index 593590f7..ac34a5f4 100644 --- a/launcher/ui/pages/instance/ManagedPackPage.cpp +++ b/launcher/ui/pages/instance/ManagedPackPage.cpp @@ -41,8 +41,38 @@ class NoBigComboBoxStyle : public QProxyStyle { return QProxyStyle::styleHint(hint, option, widget, returnData); } // clang-format on + + static NoBigComboBoxStyle* GetInstance(QStyle* style); + + private: + static QMap<QStyle*, NoBigComboBoxStyle*> s_singleton_instances_; + static std::mutex s_singleton_instances_mutex_; }; +QMap<QStyle*, NoBigComboBoxStyle*> NoBigComboBoxStyle::s_singleton_instances_ = {}; +std::mutex NoBigComboBoxStyle::s_singleton_instances_mutex_; + +/** + * QProxyStyle and QStyle objects object to being freed even if all the widgets using them are gone + * so make singlestons tied to the lifetime of the application to clean them up and ensure they arn't + * being remade over and over agian leaking memory. + * */ +NoBigComboBoxStyle* NoBigComboBoxStyle::GetInstance(QStyle* style) +{ + std::lock_guard<std::mutex> lock(s_singleton_instances_mutex_); + auto inst_iter = s_singleton_instances_.constFind(style); + NoBigComboBoxStyle* inst = nullptr; + if(inst_iter == s_singleton_instances_.constEnd() || *inst_iter == nullptr) { + inst = new NoBigComboBoxStyle(style); + inst->setParent(APPLICATION); + s_singleton_instances_.insert(style, inst); + qDebug() << "QProxyStyle NoBigComboBox created for" << style->objectName() << style; + } else { + inst = *inst_iter; + } + return inst; +} + ManagedPackPage* ManagedPackPage::createPage(BaseInstance* inst, QString type, QWidget* parent) { if (type == "modrinth") @@ -63,8 +93,7 @@ ManagedPackPage::ManagedPackPage(BaseInstance* inst, InstanceWindow* instance_wi // NOTE: GTK2 themes crash with the proxy style. // This seems like an upstream bug, so there's not much else that can be done. if (!QStyleFactory::keys().contains("gtk2")){ - auto comboStyle = new NoBigComboBoxStyle(ui->versionsComboBox->style()); - comboStyle->setParent(APPLICATION); // make sure this gets cleaned up (setting to simply `this` causes it to be freed too soon) + auto comboStyle = NoBigComboBoxStyle::GetInstance(ui->versionsComboBox->style()); ui->versionsComboBox->setStyle(comboStyle); } |