Skip to content

Commit

Permalink
Fix memory leak when unregistering a gesture recognizer
Browse files Browse the repository at this point in the history
When a gesture was unrecognized, then it would add itself to the
obsolete gestures hash. This was cleaned up only on application exit,
but as the unregister call happens whenever a widget that had registered
gestures was deleted then the hash could grow quite considerably.

In order to ensure the original intention of the code here, we only
call unregisterGestureRecognizer() when there is a QGestureManager in
place to call it on. Otherwise it would create a memory leak in itself.

Change-Id: I2342f3f737b28be4af7ed531d83f02197eb66c0e
Reviewed-by: Richard Moe Gustavsen <[email protected]>
  • Loading branch information
AndyShawQt committed Mar 19, 2019
1 parent 01380dc commit 1320b2f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 0 additions & 5 deletions src/widgets/kernel/qgesturemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,6 @@ Qt::GestureType QGestureManager::registerGestureRecognizer(QGestureRecognizer *r
void QGestureManager::unregisterGestureRecognizer(Qt::GestureType type)
{
QList<QGestureRecognizer *> list = m_recognizers.values(type);
while (QGestureRecognizer *recognizer = m_recognizers.take(type)) {
// ensuring an entry exists causes the recognizer to be deleted on destruction of the manager
auto &gestures = m_obsoleteGestures[recognizer];
Q_UNUSED(gestures);
}
foreach (QGesture *g, m_gestureToRecognizer.keys()) {
QGestureRecognizer *recognizer = m_gestureToRecognizer.value(g);
if (list.contains(recognizer)) {
Expand Down
6 changes: 6 additions & 0 deletions src/widgets/kernel/qgesturerecognizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#include "private/qgesture_p.h"
#include "private/qgesturemanager_p.h"
#include "private/qapplication_p.h"

#ifndef QT_NO_GESTURES

Expand Down Expand Up @@ -231,6 +232,11 @@ Qt::GestureType QGestureRecognizer::registerRecognizer(QGestureRecognizer *recog
*/
void QGestureRecognizer::unregisterRecognizer(Qt::GestureType type)
{
auto qAppPriv = QApplicationPrivate::instance();
if (!qAppPriv)
return;
if (!qAppPriv->gestureManager)
return;
QGestureManager::instance()->unregisterGestureRecognizer(type);
}

Expand Down

0 comments on commit 1320b2f

Please sign in to comment.