Skip to content

Commit 0a5625f

Browse files
committed
fix(test): fix memory leaks in test code
Fixed memory leaks in test code by adding proper destructors and cleanup: 1. Added destructor to TestModelA to release DataA objects stored in m_list 2. Added destructor to TestModelB to release DataB objects stored in m_list 3. Fixed QAbstractItemModelTester memory leak in RoleGroupModel.ModelTest by adding delete statement Also added AddressSanitizer and UndefinedBehaviorSanitizer flags to CMake configuration for memory leak detection: - Added -fsanitize=undefined,address to CXX_FLAGS and C_FLAGS - Added -O0 -Wall -g -ggdb3 for debug builds - Applied flags to both main CMakeLists.txt and tests/CMakeLists.txt Log: Fixed memory leaks in test code and added sanitizer flags Influence: 1. Run RoleCombineModel tests to verify no memory leaks 2. Run RoleGroupModel tests to verify QAbstractItemModelTester cleanup 3. Verify AddressSanitizer detects no leaks in test execution 4. Test that debug build works with new compiler flags 5. Verify all existing tests still pass with sanitizer enabled fix(test): 修复测试代码中的内存泄漏 通过添加适当的析构函数和清理代码修复测试代码中的内存泄漏: 1. 为 TestModelA 添加析构函数以释放 m_list 中存储的 DataA 对象 2. 为 TestModelB 添加析构函数以释放 m_list 中存储的 DataB 对象 3. 在 RoleGroupModel.ModelTest 中添加 delete 语句修复 QAbstractItemModelTester 内存泄漏 同时向 CMake 配置添加了 AddressSanitizer 和 UndefinedBehaviorSanitizer 标志 用于内存泄漏检测: - 向 CXX_FLAGS 和 C_FLAGS 添加 -fsanitize=undefined,address - 添加 -O0 -Wall -g -ggdb3 用于调试构建 - 将标志应用到主 CMakeLists.txt 和 tests/CMakeLists.txt Log: 修复测试代码内存泄漏并添加 sanitizer 标志 Influence: 1. 运行 RoleCombineModel 测试验证无内存泄漏 2. 运行 RoleGroupModel 测试验证 QAbstractItemModelTester 清理 3. 验证 AddressSanitizer 在测试执行中未检测到泄漏 4. 测试调试构建在新编译器标志下正常工作 5. 验证所有现有测试在启用 sanitizer 后仍能通过
1 parent cfd112d commit 0a5625f

8 files changed

Lines changed: 31 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
2424
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
2525
option(BUILD_WITH_X11 "Build X11 emulation" ON)
2626

27+
# Memory leak detection and debugging flags
28+
set(CMAKE_CXX_FLAGS "-O0 -fsanitize=undefined,address -Wall -g -ggdb3")
29+
set(CMAKE_C_FLAGS "-O0 -fsanitize=undefined,address -Wall -g -ggdb3")
30+
set(CMAKE_EXE_LINKER_FLAGS "-fsanitize=undefined,address")
31+
set(CMAKE_SHARED_LINKER_FLAGS "-fsanitize=undefined,address")
32+
2733
set(DS_BUILD_WITH_QT6 ON CACHE BOOL "Build dde-shell with Qt6")
2834

2935
if (DS_BUILD_WITH_QT6)

tests/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@
22
#
33
# SPDX-License-Identifier: CC0-1.0
44

5+
# Memory leak detection and debugging flags for tests
6+
set(CMAKE_CXX_FLAGS "-O0 -fsanitize=undefined,address -Wall -g -ggdb3")
7+
set(CMAKE_C_FLAGS "-O0 -fsanitize=undefined,address -Wall -g -ggdb3")
8+
set(CMAKE_EXE_LINKER_FLAGS "-fsanitize=undefined,address")
9+
set(CMAKE_SHARED_LINKER_FLAGS "-fsanitize=undefined,address")
10+
511
add_subdirectory(panels)

tests/panels/dock/taskmanager/combinemodela.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ TestModelA::TestModelA(QObject *parent)
4343

4444
}
4545

46+
TestModelA::~TestModelA()
47+
{
48+
qDeleteAll(m_list);
49+
m_list.clear();
50+
}
51+
4652
QHash<int, QByteArray> TestModelA::roleNames() const
4753
{
4854
return {

tests/panels/dock/taskmanager/combinemodela.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class TestModelA : public QAbstractListModel
3434
};
3535
Q_ENUM(Roles)
3636
TestModelA(QObject *parent = nullptr);
37+
~TestModelA();
3738
QHash<int, QByteArray> roleNames() const override;
3839
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
3940
QVariant data(const QModelIndex &index, int role) const override;

tests/panels/dock/taskmanager/combinemodelb.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ TestModelB::TestModelB(QObject *parent)
4343

4444
}
4545

46+
TestModelB::~TestModelB()
47+
{
48+
qDeleteAll(m_list);
49+
m_list.clear();
50+
}
51+
4652
QHash<int, QByteArray> TestModelB::roleNames() const
4753
{
4854
return {

tests/panels/dock/taskmanager/combinemodelb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class TestModelB : public QAbstractListModel
3333
};
3434
Q_ENUM(Roles)
3535
TestModelB(QObject *parent = nullptr);
36+
~TestModelB();
3637
QHash<int, QByteArray> roleNames() const override;
3738
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
3839
QVariant data(const QModelIndex &index, int role) const override;

tests/panels/dock/taskmanager/rolecombinemodeltests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ TEST(RoleGroupModel, ModelTest)
6868
return QModelIndex();
6969
});
7070

71-
[[maybe_unused]] auto tester = new QAbstractItemModelTester(&model, QAbstractItemModelTester::FailureReportingMode::Fatal);
71+
auto tester = new QAbstractItemModelTester(&model, QAbstractItemModelTester::FailureReportingMode::Fatal);
72+
delete tester;
7273
}
7374

7475
TEST(RoleCombineModel, dataTest) {

tests/panels/notification/server/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ add_executable(notifyserverapplet_tests
2828
notifyserverapplet_test.cpp
2929
)
3030

31-
target_link_libraries(notifyserverapplet_tests
31+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden")
32+
33+
target_link_libraries(notifyserverapplet_tests PRIVATE
3234
GTest::GTest
3335
GTest::gmock
3436
GTest::gmock_main

0 commit comments

Comments
 (0)