Use deleteLater() when destroying lazy initialized objects

Fixes #530
This commit is contained in:
Jonas Kvinge
2020-09-04 20:25:46 +02:00
parent 9cb305fb0d
commit 71dc47d6c9
2 changed files with 8 additions and 6 deletions

View File

@@ -21,7 +21,7 @@
#include <functional> #include <functional>
#include <memory> #include <memory>
// Helper for lazy initialisation of objects. // Helper for lazy initialization of objects.
// Usage: // Usage:
// Lazy<Foo> my_lazy_object([]() { return new Foo; }); // Lazy<Foo> my_lazy_object([]() { return new Foo; });
@@ -48,19 +48,18 @@ class Lazy {
// Returns true if the object is not yet initialised. // Returns true if the object is not yet initialised.
explicit operator bool() const { return ptr_; } explicit operator bool() const { return ptr_; }
// Deletes the underlying object and will re-run the initialisation function // Deletes the underlying object and will re-run the initialisation function if the object is requested again.
// if the object is requested again. void reset() { ptr_.reset(); }
void reset() { ptr_.reset(nullptr); }
private: private:
void CheckInitialised() const { void CheckInitialised() const {
if (!ptr_) { if (!ptr_) {
ptr_.reset(init_()); ptr_.reset(init_(), [](T*obj) { obj->deleteLater(); });
} }
} }
const std::function<T*()> init_; const std::function<T*()> init_;
mutable std::unique_ptr<T> ptr_; mutable std::shared_ptr<T> ptr_;
}; };
#endif // LAZY_H #endif // LAZY_H

View File

@@ -1087,6 +1087,9 @@ void MainWindow::SaveSettings() {
void MainWindow::Exit() { void MainWindow::Exit() {
// Make sure Settings dialog is destroyed first.
settings_dialog_.reset();
++exit_count_; ++exit_count_;
SaveSettings(); SaveSettings();