From f00e8ba18e4978e2c4ac9bcb0d980c13455975ca Mon Sep 17 00:00:00 2001 From: thunder2 Date: Thu, 28 Dec 2023 17:05:29 +0100 Subject: [PATCH] Fixed memory leak when using tray menu of chat lobby and removed double connect of signals --- .../src/gui/chat/ChatLobbyUserNotify.cpp | 28 +++++++------------ .../src/gui/chat/ChatLobbyUserNotify.h | 1 + retroshare-gui/src/gui/chat/ChatWidget.cpp | 3 +- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/retroshare-gui/src/gui/chat/ChatLobbyUserNotify.cpp b/retroshare-gui/src/gui/chat/ChatLobbyUserNotify.cpp index 3d2679628..719ad7d2d 100644 --- a/retroshare-gui/src/gui/chat/ChatLobbyUserNotify.cpp +++ b/retroshare-gui/src/gui/chat/ChatLobbyUserNotify.cpp @@ -187,7 +187,7 @@ void ChatLobbyUserNotify::iconClicked() #else /// Tray icon Menu /// - QMenu* trayMenu = new QMenu(MainWindow::getInstance()); + QMenu* trayMenu = createMenu(); std::list lobbies; rsMsgs->getChatLobbyList(lobbies); bool doUpdate=false; @@ -226,30 +226,27 @@ void ChatLobbyUserNotify::iconClicked() } } - if (notifyCombined()) { - QSystemTrayIcon* trayIcon=getTrayIcon(); - if (trayIcon!=NULL) trayIcon->setContextMenu(trayMenu); - } else { - QAction* action=getNotifyIcon(); - if (action!=NULL) { - action->setMenu(trayMenu); - } - } - QString strName=tr("Remove All"); QAction *pAction = new QAction( QIcon(), strName, trayMenu); ActionTag actionTag={0x0, "", true}; pAction->setData(qVariantFromValue(actionTag)); - connect(trayMenu, SIGNAL(triggered(QAction*)), this, SLOT(subMenuClicked(QAction*))); - connect(trayMenu, SIGNAL(hovered(QAction*)), this, SLOT(subMenuHovered(QAction*))); trayMenu->addAction(pAction); trayMenu->exec(QCursor::pos()); + delete(trayMenu); if (doUpdate) updateIcon(); #endif } +QMenu* ChatLobbyUserNotify::createMenu() +{ + QMenu* menu = new QMenu(MainWindow::getInstance()); + connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(subMenuClicked(QAction*))); + connect(menu, SIGNAL(hovered(QAction*)), this, SLOT(subMenuHovered(QAction*))); + return menu; +} + void ChatLobbyUserNotify::makeSubMenu(QMenu* parentMenu, QIcon icoLobby, QString strLobbyName, ChatLobbyId id) { lobby_map::iterator itCL=_listMsg.find(id); @@ -258,11 +255,7 @@ void ChatLobbyUserNotify::makeSubMenu(QMenu* parentMenu, QIcon icoLobby, QString unsigned int msgCount=msgMap.size(); - if(!parentMenu) parentMenu = new QMenu(MainWindow::getInstance()); QMenu *lobbyMenu = parentMenu->addMenu(icoLobby, strLobbyName); - connect(lobbyMenu, SIGNAL(triggered(QAction*)), this, SLOT(subMenuClicked(QAction*))); - connect(lobbyMenu, SIGNAL(hovered(QAction*)), this, SLOT(subMenuHovered(QAction*))); - lobbyMenu->setToolTip(getNotifyMessage(msgCount>1).arg(msgCount)); for (msg_map::iterator itMsg=msgMap.begin(); itMsg!=msgMap.end(); ++itMsg) { @@ -284,7 +277,6 @@ void ChatLobbyUserNotify::makeSubMenu(QMenu* parentMenu, QIcon icoLobby, QString ActionTag actionTag={itCL->first, "", true}; pAction->setData(qVariantFromValue(actionTag)); lobbyMenu->addAction(pAction); - } void ChatLobbyUserNotify::iconHovered() diff --git a/retroshare-gui/src/gui/chat/ChatLobbyUserNotify.h b/retroshare-gui/src/gui/chat/ChatLobbyUserNotify.h index fd95511b4..2e49f450f 100644 --- a/retroshare-gui/src/gui/chat/ChatLobbyUserNotify.h +++ b/retroshare-gui/src/gui/chat/ChatLobbyUserNotify.h @@ -49,6 +49,7 @@ public: ChatLobbyUserNotify(QObject *parent = 0); virtual bool hasSetting(QString *name, QString *group); + QMenu* createMenu(); void makeSubMenu(QMenu* parentMenu, QIcon icoLobby, QString strLobbyName, ChatLobbyId id); void chatLobbyNewMessage(ChatLobbyId lobby_id, QDateTime time, QString senderName, QString msg); void chatLobbyCleared(ChatLobbyId lobby_id, QString anchor, bool onlyUnread=false); diff --git a/retroshare-gui/src/gui/chat/ChatWidget.cpp b/retroshare-gui/src/gui/chat/ChatWidget.cpp index 03f954964..abab8336a 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.cpp +++ b/retroshare-gui/src/gui/chat/ChatWidget.cpp @@ -982,11 +982,12 @@ void ChatWidget::on_notifyButton_clicked() if(!notify) return; if (chatType() != CHATTYPE_LOBBY) return; - QMenu* menu = new QMenu(MainWindow::getInstance()); + QMenu* menu = notify->createMenu(); QIcon icoLobby=(ui->notifyButton->icon()); notify->makeSubMenu(menu, icoLobby, title, chatId.toLobbyId()); menu->exec(ui->notifyButton->mapToGlobal(QPoint(0,ui->notifyButton->geometry().height()))); + delete(menu); }