From 63029d85d739eaa1b6c1a7fbf783215c6a852daf Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Tue, 6 Aug 2013 02:34:03 +0200 Subject: [PATCH] Use shared_ptr for layers This allows transient layers to be deleted automatically when they are dismissed, while persistent layers will be kept alive by their other owner(s). --- src/gmenu2x.cpp | 19 +++++-------------- src/gmenu2x.h | 6 +++--- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index 7928a78..6eabc48 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -231,8 +231,6 @@ GMenu2X::GMenu2X() bg = NULL; font = NULL; - menu = NULL; - helpPopup = nullptr; btnContextMenu = nullptr; setSkin(confStr["skin"], !fileExists(confStr["wallpaper"])); initMenu(); @@ -256,7 +254,7 @@ GMenu2X::GMenu2X() DEBUG("Loading system input.conf file: %s.\n", input_file.c_str()); } - input.init(input_file, menu); + input.init(input_file, menu.get()); if (confInt["backlightTimeout"] > 0) PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] ); @@ -279,8 +277,6 @@ GMenu2X::~GMenu2X() { delete Clock::getInstance(); quit(); - delete menu; - delete helpPopup; delete btnContextMenu; delete font; delete monitor; @@ -372,7 +368,7 @@ void GMenu2X::initFont() { void GMenu2X::initMenu() { //Menu structure handler - menu = new Menu(this, ts); + menu.reset(new Menu(this, ts)); for (uint i=0; igetSections().size(); i++) { //Add virtual links in the applications section if (menu->getSections()[i]=="applications") { @@ -608,7 +604,7 @@ void GMenu2X::paint() { //Background sc["bgmain"]->blit(s,0,0); - for (Layer *layer : layers) { + for (auto layer : layers) { layer->paint(*s); } @@ -679,8 +675,7 @@ void GMenu2X::main() { if (!handled) { switch (button) { case InputManager::CANCEL: - helpPopup = new HelpPopup(*this); - layers.push_back(helpPopup); + layers.push_back(make_shared(*this)); break; case InputManager::SETTINGS: options(); @@ -694,13 +689,9 @@ void GMenu2X::main() { } for (auto it = layers.begin(); it != layers.end(); ) { - Layer *layer = *it; + auto layer = *it; if (layer->wasDismissed()) { it = layers.erase(it); - if (layer == helpPopup) { - delete helpPopup; - helpPopup = nullptr; - } } else { ++it; } diff --git a/src/gmenu2x.h b/src/gmenu2x.h index 4ca7e19..7dff664 100644 --- a/src/gmenu2x.h +++ b/src/gmenu2x.h @@ -28,6 +28,7 @@ #include "surface.h" #include +#include #include #include #include @@ -67,12 +68,11 @@ typedef std::unordered_map > ConfIntHas class GMenu2X { private: Touchscreen ts; - Menu *menu; - HelpPopup *helpPopup; + std::shared_ptr menu; MediaMonitor *monitor; std::string batteryIcon; - std::vector layers; + std::vector> layers; /*! Retrieves the free disk space on the sd