From 71dc47d6c91cfb2bc884fe81746d78ededc881dd Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Fri, 4 Sep 2020 20:25:46 +0200 Subject: [PATCH] Use deleteLater() when destroying lazy initialized objects Fixes #530 --- ext/libstrawberry-common/core/lazy.h | 11 +++++------ src/core/mainwindow.cpp | 3 +++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/ext/libstrawberry-common/core/lazy.h b/ext/libstrawberry-common/core/lazy.h index 53ee4b1ef..90f1f7394 100644 --- a/ext/libstrawberry-common/core/lazy.h +++ b/ext/libstrawberry-common/core/lazy.h @@ -21,7 +21,7 @@ #include #include -// Helper for lazy initialisation of objects. +// Helper for lazy initialization of objects. // Usage: // Lazy my_lazy_object([]() { return new Foo; }); @@ -48,19 +48,18 @@ class Lazy { // Returns true if the object is not yet initialised. explicit operator bool() const { return ptr_; } - // Deletes the underlying object and will re-run the initialisation function - // if the object is requested again. - void reset() { ptr_.reset(nullptr); } + // Deletes the underlying object and will re-run the initialisation function if the object is requested again. + void reset() { ptr_.reset(); } private: void CheckInitialised() const { if (!ptr_) { - ptr_.reset(init_()); + ptr_.reset(init_(), [](T*obj) { obj->deleteLater(); }); } } const std::function init_; - mutable std::unique_ptr ptr_; + mutable std::shared_ptr ptr_; }; #endif // LAZY_H diff --git a/src/core/mainwindow.cpp b/src/core/mainwindow.cpp index 700c1df6e..cc14b2145 100644 --- a/src/core/mainwindow.cpp +++ b/src/core/mainwindow.cpp @@ -1087,6 +1087,9 @@ void MainWindow::SaveSettings() { void MainWindow::Exit() { + // Make sure Settings dialog is destroyed first. + settings_dialog_.reset(); + ++exit_count_; SaveSettings();