-
Notifications
You must be signed in to change notification settings - Fork 70
fix(notification): fix memory leak in NotifyServerApplet destructor #1598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,22 +2,43 @@ | |
| // | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <gmock/gmock.h> | ||
|
|
||
| #include <atomic> | ||
| #include <QCoreApplication> | ||
| #include <QPointer> | ||
| #include <QTest> | ||
| #include <QThread> | ||
| #include <QSignalSpy> | ||
| #include <QVariant> | ||
|
|
||
| #define private public | ||
| #include "notifyserverapplet.h" | ||
| #undef private | ||
| #include "notificationmanager.h" | ||
|
|
||
| using namespace notification; | ||
| using ::testing::_; | ||
| using ::testing::Return; | ||
| using ::testing::Invoke; | ||
|
|
||
| class TrackableNotificationManager : public NotificationManager { | ||
| Q_OBJECT | ||
| public: | ||
| explicit TrackableNotificationManager(QObject *parent = nullptr) | ||
| : NotificationManager(parent) {} | ||
|
|
||
| ~TrackableNotificationManager() override | ||
| { | ||
| ++destroyedCount; | ||
| } | ||
|
|
||
| static std::atomic_int destroyedCount; | ||
| }; | ||
|
|
||
| std::atomic_int TrackableNotificationManager::destroyedCount = 0; | ||
|
|
||
| // Mock class for NotificationManager | ||
| class MockNotificationManager : public NotificationManager { | ||
| Q_OBJECT | ||
|
|
@@ -72,6 +93,59 @@ | |
| EXPECT_NO_THROW(delete testApplet); | ||
| } | ||
|
|
||
| // Test destructor when only manager exists (no worker thread) | ||
| TEST_F(NotifyServerAppletTest, DestructorWithManagerOnlyTest) { | ||
| TrackableNotificationManager::destroyedCount = 0; | ||
|
|
||
| auto *testApplet = new NotifyServerApplet(); | ||
| auto *manager = new TrackableNotificationManager(); | ||
| QPointer<TrackableNotificationManager> managerGuard(manager); | ||
|
|
||
| testApplet->m_manager = manager; | ||
| // m_worker remains nullptr | ||
|
|
||
| delete testApplet; | ||
|
|
||
| EXPECT_TRUE(managerGuard.isNull()); | ||
| EXPECT_EQ(TrackableNotificationManager::destroyedCount.load(), 1); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): The null QPointer and destroyedCount checks may race with an asynchronous deleteLater, making this test potentially flaky. Because the manager is deleted via a signal/ |
||
| } | ||
|
|
||
| // Test destructor when worker exists but manager is null | ||
| TEST_F(NotifyServerAppletTest, DestructorWithWorkerOnlyTest) { | ||
| auto *testApplet = new NotifyServerApplet(); | ||
| auto *worker = new QThread(); | ||
|
|
||
| testApplet->m_worker = worker; | ||
| testApplet->m_manager = nullptr; | ||
|
|
||
| worker->start(); | ||
| QTRY_VERIFY_WITH_TIMEOUT(worker->isRunning(), 1000); | ||
|
|
||
| EXPECT_NO_THROW(delete testApplet); | ||
| } | ||
|
|
||
| TEST_F(NotifyServerAppletTest, DestructorDeletesManagerAfterWorkerThreadExit) { | ||
| TrackableNotificationManager::destroyedCount = 0; | ||
|
|
||
| auto *testApplet = new NotifyServerApplet(); | ||
| auto *worker = new QThread(); | ||
| auto *manager = new TrackableNotificationManager(); | ||
| QPointer<TrackableNotificationManager> managerGuard(manager); | ||
|
|
||
| testApplet->m_manager = manager; | ||
| testApplet->m_worker = worker; | ||
|
|
||
| manager->moveToThread(worker); | ||
| worker->start(); | ||
|
|
||
| QTRY_VERIFY_WITH_TIMEOUT(worker->isRunning(), 1000); | ||
|
|
||
| delete testApplet; | ||
|
|
||
| EXPECT_TRUE(managerGuard.isNull()); | ||
| EXPECT_EQ(TrackableNotificationManager::destroyedCount.load(), 1); | ||
| } | ||
|
|
||
| // Test memory leak detection for destructor with initialized applet | ||
| TEST_F(NotifyServerAppletTest, DestructorMemoryLeakTest) { | ||
| // This test verifies that all resources are properly cleaned up | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): QThread::finished → deleteLater on an object in the worker thread is likely never processed after calling exit(), leading to a leak.
Because
QThread::finishedis emitted in the owning (likely main/UI) thread, thedeleteLatercall onm_managerbecomes a queued event to the worker’s event loop. Butexit()stops that event loop andfinishedis emitted only after it has already exited, so the queueddeleteLatermay never run, leakingm_manager.Instead, delete
m_managerbefore shutting down the worker, for example by:QMetaObject::invokeMethod(m_manager, "deleteLater", Qt::BlockingQueuedConnection)beforem_worker->exit()/wait(), orThe key point is to clean up
m_managersynchronously in the correct thread, rather than relying onfinishedafter the event loop is gone.