From 463cc80041e9b5614d6fd4b678e4ac5a2a041430 Mon Sep 17 00:00:00 2001 From: deepin-wm Date: Thu, 18 Jun 2026 19:01:47 +0800 Subject: [PATCH] fix: fix destroyed callback issues in GestureRecognizer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Replace destroyed callback with direct list cleanup lambda to avoid calling unregister on a being-destroyed object which emits cancelled signal and calls deleteLater 2. Remove m_destroyConnections as Qt auto-disconnects destroyed signal 3. Fix unregisterHoldGesture to clean m_activeHoldGestures and emit cancelled only when gesture was active, matching swipe behavior 4. Remove deleteLater in HoldGesture destructor as child objects are auto-destroyed by Qt parent mechanism Log: Fixed unsafe destroyed callback behavior in gesture recognition Influence: 1. Test gesture unregister during active gesture recognition 2. Test HoldGesture lifecycle and timer behavior 3. Test SwipeGesture unregister with active gestures fix: 修复GestureRecognizer中destroyed回调相关问题 1. 将destroyed回调改为直接列表清理lambda,避免在对象销毁过程中调用unregister导致发射cancelled信号和deleteLater 2. 移除m_destroyConnections,Qt会自动断开destroyed信号连接 3. 修复unregisterHoldGesture补充m_activeHoldGestures清理,仅在手势活跃时发射cancelled,与swipe版本对称 4. 移除HoldGesture析构函数中的deleteLater,子对象由Qt父对象机制自动销毁 Log: 修复手势识别中destroyed回调的不安全行为 Influence: 1. 测试手势识别期间的手势注销 2. 测试HoldGesture生命周期和定时器行为 3. 测试活跃状态下SwipeGesture的注销 Fixes: #WM-32 --- src/input/gestures.cpp | 34 +++++++++++----------------------- src/input/gestures.h | 3 +-- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/input/gestures.cpp b/src/input/gestures.cpp index 3a1aa3e98..031e0b106 100644 --- a/src/input/gestures.cpp +++ b/src/input/gestures.cpp @@ -179,22 +179,16 @@ GestureRecognizer::GestureRecognizer(QObject *parent) void GestureRecognizer::registerSwipeGesture(SwipeGesture *gesture) { Q_ASSERT(!m_swipeGestures.contains(gesture)); - auto connection = connect(gesture, - &QObject::destroyed, - this, - std::bind(&GestureRecognizer::unregisterSwipeGesture, this, gesture)); - m_destroyConnections.insert(gesture, connection); + connect(gesture, &QObject::destroyed, this, [this, gesture] { + m_swipeGestures.removeOne(gesture); + m_activeSwipeGestures.removeOne(gesture); + }); m_swipeGestures << gesture; } void GestureRecognizer::unregisterSwipeGesture(SwipeGesture *gesture) { - auto it = m_destroyConnections.find(gesture); - if (it != m_destroyConnections.end()) { - disconnect(it.value()); - m_destroyConnections.erase(it); - } - m_swipeGestures.removeAll(gesture); + m_swipeGestures.removeOne(gesture); if (m_activeSwipeGestures.removeOne(gesture)) { Q_EMIT gesture->cancelled(); } @@ -371,7 +365,6 @@ HoldGesture::~HoldGesture() { if (m_holdTimer != nullptr) { m_holdTimer->stop(); - m_holdTimer->deleteLater(); } } @@ -398,22 +391,17 @@ bool HoldGesture::isActive() const void GestureRecognizer::registerHoldGesture(HoldGesture *gesture) { Q_ASSERT(!m_holdGestures.contains(gesture)); - auto connection = connect(gesture, - &QObject::destroyed, - this, - std::bind(&GestureRecognizer::unregisterHoldGesture, this, gesture)); - m_destroyConnections.insert(gesture, connection); + connect(gesture, &QObject::destroyed, this, [this, gesture] { + m_holdGestures.removeOne(gesture); + m_activeHoldGestures.removeOne(gesture); + }); m_holdGestures << gesture; } void GestureRecognizer::unregisterHoldGesture(HoldGesture *gesture) { - auto it = m_destroyConnections.find(gesture); - if (it != m_destroyConnections.end()) { - disconnect(it.value()); - m_destroyConnections.erase(it); - } - if (m_holdGestures.removeOne(gesture)) { + m_holdGestures.removeOne(gesture); + if (m_activeHoldGestures.removeOne(gesture)) { Q_EMIT gesture->cancelled(); } gesture->deleteLater(); diff --git a/src/input/gestures.h b/src/input/gestures.h index 35de72130..3a0d1ab5b 100644 --- a/src/input/gestures.h +++ b/src/input/gestures.h @@ -1,5 +1,5 @@ -// Copyright (C) 2024-2025 UnionTech Software Technology Co., Ltd. +// Copyright (C) 2024-2026 UnionTech Software Technology Co., Ltd. // SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #pragma once @@ -158,7 +158,6 @@ class GestureRecognizer : public QObject QList m_activeSwipeGestures; QList m_holdGestures; QList m_activeHoldGestures; - QMap m_destroyConnections; QPointF m_currentDelta = QPointF(0, 0); uint m_currentFingerCount = 0;