Skip to content

Commit 903d956

Browse files
committed
fix: fix network details management with pointer-based tracking
Changed the network details management from using name strings to using object pointers as unique identifiers. Previously, using names could cause issues when multiple network details had the same name. Now we use NetworkDetails object pointers to uniquely identify each details item, ensuring proper tracking and cleanup. The updateDetails() function now: 1. Uses NetworkDetails* pointers as keys in m_detailsItemsMap instead of name strings 2. Generates unique IDs using the object pointer address (detail_<pointer_address>) 3. Properly identifies items to remove by comparing current NetworkDetails objects with stored ones 4. Removes items when their NetworkDetails objects are no longer in the controller's list 5. Adds new items when NetworkDetails objects appear in the controller's list 6. Updates data for all existing items using their unique IDs This prevents issues where network details with duplicate names could cause incorrect item removal or data updates. Log: Fixed network details display issues when multiple networks have the same name Influence: 1. Test network details display with multiple networks 2. Verify details are correctly added and removed when networks appear/ disappear 3. Test scenarios where networks might have identical names 4. Verify data updates correctly when network information changes 5. Test network switching and ensure details update properly 6. Check memory management to ensure no leaks from pointer-based tracking fix: 修复网络详情管理,使用基于指针的跟踪机制 将网络详情管理从使用名称字符串改为使用对象指针作为唯一标识符。之前使用名 称可能导致多个网络详情具有相同名称时出现问题。现在使用 NetworkDetails 对 象指针来唯一标识每个详情项,确保正确的跟踪和清理。 updateDetails() 函数现在: 1. 使用 NetworkDetails* 指针作为 m_detailsItemsMap 的键,而不是名称字 符串 2. 使用对象指针地址生成唯一ID(detail_<指针地址>) 3. 通过比较当前 NetworkDetails 对象与存储的对象来正确识别需要删除的项 4. 当 NetworkDetails 对象不再存在于控制器列表中时删除相应项 5. 当 NetworkDetails 对象出现在控制器列表中时添加新项 6. 使用唯一ID为所有现有项更新数据 这防止了当网络详情具有重复名称时可能导致错误项删除或数据更新的问题。 Log: 修复了当多个网络具有相同名称时的网络详情显示问题 Influence: 1. 测试多个网络时的网络详情显示 2. 验证网络出现/消失时详情是否正确添加和删除 3. 测试网络可能具有相同名称的场景 4. 验证网络信息变化时数据是否正确更新 5. 测试网络切换并确保详情正确更新 6. 检查内存管理,确保基于指针的跟踪不会导致内存泄漏 PMS: BUG-342457
1 parent 8e33b75 commit 903d956

2 files changed

Lines changed: 45 additions & 13 deletions

File tree

net-view/operation/private/netmanagerthreadprivate.cpp

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,32 +2401,62 @@ void NetManagerThreadPrivate::onDslActiveConnectionChanged()
24012401

24022402
void NetManagerThreadPrivate::updateDetails()
24032403
{
2404-
QSet<QString> detailsItems = m_detailsItems;
2404+
// 使用对象指针作为键,而不是 name()
2405+
QSet<NetworkDetails*> currentDetails;
2406+
for (auto &&details : NetworkController::instance()->networkDetails()) {
2407+
if (!details) // 空指针检查
2408+
continue;
2409+
currentDetails.insert(details);
2410+
}
2411+
2412+
// 找出需要删除的项(在 m_detailsItemsMap 中但不在 currentDetails 中)
2413+
QList<NetworkDetails*> toRemove;
2414+
for (auto it = m_detailsItemsMap.begin(); it != m_detailsItemsMap.end(); ++it) {
2415+
if (!currentDetails.contains(it.key())) {
2416+
toRemove.append(it.key());
2417+
}
2418+
}
2419+
2420+
// 删除不再存在的项
2421+
for (auto *details : toRemove) {
2422+
QString itemId = m_detailsItemsMap.value(details);
2423+
m_detailsItemsMap.remove(details);
2424+
// 断开信号连接,避免连接泄漏
2425+
disconnect(details, &NetworkDetails::infoChanged, this, &NetManagerThreadPrivate::updateDetails);
2426+
Q_EMIT itemRemoved(itemId);
2427+
}
2428+
2429+
// 添加新项或更新现有项
24052430
int index = 0;
24062431
for (auto &&details : NetworkController::instance()->networkDetails()) {
2407-
if (detailsItems.contains(details->name())) {
2408-
detailsItems.remove(details->name());
2409-
} else {
2410-
m_detailsItems.insert(details->name());
2432+
if (!details) // 空指针检查
2433+
continue;
2434+
2435+
// 生成唯一ID:使用对象指针地址
2436+
QString uniqueId = QString("detail_%1").arg(reinterpret_cast<quintptr>(details));
2437+
2438+
if (!m_detailsItemsMap.contains(details)) {
2439+
// 新项,创建 NetDetailsInfoItem
2440+
m_detailsItemsMap.insert(details, uniqueId);
24112441
connect(details, &NetworkDetails::infoChanged, this, &NetManagerThreadPrivate::updateDetails, Qt::QueuedConnection);
2412-
NetDetailsInfoItemPrivate *item = NetItemNew(DetailsInfoItem, details->name());
2442+
2443+
NetDetailsInfoItemPrivate *item = NetItemNew(DetailsInfoItem, uniqueId);
24132444
item->updatename(details->name());
24142445
item->updateindex(index);
24152446
item->item()->moveToThread(m_parentThread);
2447+
24162448
Q_EMIT itemAdded("Details", item);
24172449
}
2450+
2451+
// 更新数据
24182452
QList<QStringList> data;
24192453
for (auto &&info : details->items()) {
24202454
data.append({ info.first, info.second });
24212455
}
2422-
Q_EMIT dataChanged(DataChanged::DetailsChanged, details->name(), QVariant::fromValue(data));
2423-
Q_EMIT dataChanged(DataChanged::IndexChanged, details->name(), QVariant::fromValue(index));
2456+
Q_EMIT dataChanged(DataChanged::DetailsChanged, uniqueId, QVariant::fromValue(data));
2457+
Q_EMIT dataChanged(DataChanged::IndexChanged, uniqueId, QVariant::fromValue(index));
24242458
++index;
24252459
}
2426-
for (auto &&item : detailsItems) {
2427-
m_detailsItems.remove(item);
2428-
Q_EMIT itemRemoved(item);
2429-
}
24302460
}
24312461

24322462
void NetManagerThreadPrivate::updateAutoScan()

net-view/operation/private/netmanagerthreadprivate.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <NetworkManagerQt/WirelessSecuritySetting>
1212

1313
#include <QObject>
14+
#include <QMap>
1415

1516
class QTimer;
1617

@@ -27,6 +28,7 @@ class VPNItem;
2728
class DSLItem;
2829
class NetDeviceItemPrivate;
2930
class NetSecretAgentInterface;
31+
class NetworkDetails;
3032
enum class NetConnectionStatus;
3133
enum class NetworkNotifyType;
3234
enum class ProxyMethod;
@@ -284,7 +286,7 @@ protected Q_SLOTS:
284286
bool m_airplaneModeEnabled;
285287
bool m_isSleeping;
286288
QString m_serverKey;
287-
QSet<QString> m_detailsItems;
289+
QMap<NetworkDetails*, QString> m_detailsItemsMap; // 存储 NetworkDetails 指针到唯一ID的映射
288290
QString m_showPageCmd;
289291
QTimer *m_showPageTimer;
290292
QString m_newVPNuuid;

0 commit comments

Comments
 (0)