From 41e928239d3735ac4a6902f6b11b6be4902e4f03 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Thu, 23 Mar 2017 17:25:42 +0100 Subject: [PATCH] fix(src/app/App): avoid memory leaks (system tray icon, qml file selector) --- linphone-desktop/src/app/App.cpp | 22 +++++++++------------- linphone-desktop/src/app/App.hpp | 3 +++ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/linphone-desktop/src/app/App.cpp b/linphone-desktop/src/app/App.cpp index d9b1230c6..1d08556d8 100644 --- a/linphone-desktop/src/app/App.cpp +++ b/linphone-desktop/src/app/App.cpp @@ -90,6 +90,7 @@ App::~App () { qInfo() << "Destroying app..."; delete m_calls_window; delete m_settings_window; + delete m_system_tray_icon; } // ----------------------------------------------------------------------------- @@ -100,11 +101,7 @@ void App::initContentApp () { qInfo() << "Activated selectors:" << QQmlFileSelector::get(&m_engine)->selector()->allSelectors(); // Provide `+custom` folders for custom components. - { - QQmlFileSelector *file_selector = new QQmlFileSelector(&m_engine); - file_selector = new QQmlFileSelector(&m_engine); - file_selector->setExtraSelectors(QStringList("custom")); - } + (new QQmlFileSelector(&m_engine, this))->setExtraSelectors(QStringList("custom")); // Set modules paths. m_engine.addImportPath(":/ui/modules"); @@ -332,10 +329,8 @@ void App::registerTypes () { // ----------------------------------------------------------------------------- void App::setTrayIcon () { + QSystemTrayIcon *m_system_tray_icon = new QSystemTrayIcon(); QQuickWindow *root = getMainWindow(); - QMenu *menu = new QMenu(); - - QSystemTrayIcon *system_tray_icon = new QSystemTrayIcon(root); // trayIcon: Right click actions. QAction *quit_action = new QAction("Quit", root); @@ -345,8 +340,9 @@ void App::setTrayIcon () { root->connect(restore_action, &QAction::triggered, root, &QQuickWindow::showNormal); // trayIcon: Left click actions. + QMenu *menu = new QMenu(); root->connect( - system_tray_icon, &QSystemTrayIcon::activated, [root]( + m_system_tray_icon, &QSystemTrayIcon::activated, [root]( QSystemTrayIcon::ActivationReason reason ) { if (reason == QSystemTrayIcon::Trigger) { @@ -363,10 +359,10 @@ void App::setTrayIcon () { menu->addSeparator(); menu->addAction(quit_action); - system_tray_icon->setContextMenu(menu); - system_tray_icon->setIcon(QIcon(WINDOW_ICON_PATH)); - system_tray_icon->setToolTip("Linphone"); - system_tray_icon->show(); + m_system_tray_icon->setContextMenu(menu); + m_system_tray_icon->setIcon(QIcon(WINDOW_ICON_PATH)); + m_system_tray_icon->setToolTip("Linphone"); + m_system_tray_icon->show(); } // ----------------------------------------------------------------------------- diff --git a/linphone-desktop/src/app/App.hpp b/linphone-desktop/src/app/App.hpp index 40d3505f2..7f3348ed0 100644 --- a/linphone-desktop/src/app/App.hpp +++ b/linphone-desktop/src/app/App.hpp @@ -33,6 +33,7 @@ // ============================================================================= class DefaultTranslator; +class QSystemTrayIcon; class App : public SingleApplication { Q_OBJECT; @@ -100,6 +101,8 @@ private: QQuickWindow *m_calls_window = nullptr; QQuickWindow *m_settings_window = nullptr; + + QSystemTrayIcon *m_system_tray_icon = nullptr; }; #endif // APP_H_