Avoid memory leaks in QWaylandInputDevice

Use QScopedPointer and QObject-parenting to get rid of the allocated
objects.

Change-Id: I8a0ce2d1bae6a69b458a01731489813ef2aa8044
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Reviewed-by: David Edmundson <davidedmundson@kde.org>
Reviewed-by: Pier Luigi Fiorini <pierluigi.fiorini@liri.io>
This commit is contained in:
Ulf Hermann 2021-08-17 13:54:01 +02:00
parent 2a966bcd13
commit fba81e2346
3 changed files with 32 additions and 40 deletions

View File

@ -64,7 +64,8 @@ QT_BEGIN_NAMESPACE
namespace QtWaylandClient { namespace QtWaylandClient {
QWaylandDataDevice::QWaylandDataDevice(QWaylandDataDeviceManager *manager, QWaylandInputDevice *inputDevice) QWaylandDataDevice::QWaylandDataDevice(QWaylandDataDeviceManager *manager, QWaylandInputDevice *inputDevice)
: QtWayland::wl_data_device(manager->get_data_device(inputDevice->wl_seat())) : QObject(inputDevice)
, QtWayland::wl_data_device(manager->get_data_device(inputDevice->wl_seat()))
, m_display(manager->display()) , m_display(manager->display())
, m_inputDevice(inputDevice) , m_inputDevice(inputDevice)
{ {

View File

@ -429,66 +429,57 @@ QWaylandInputDevice::QWaylandInputDevice(QWaylandDisplay *display, int version,
mTabletSeat.reset(new QWaylandTabletSeatV2(tm, this)); mTabletSeat.reset(new QWaylandTabletSeatV2(tm, this));
} }
QWaylandInputDevice::~QWaylandInputDevice() // Can't be in header because dtors for scoped pointers aren't known there.
{ QWaylandInputDevice::~QWaylandInputDevice() = default;
delete mPointer;
delete mKeyboard;
delete mTouch;
}
void QWaylandInputDevice::seat_capabilities(uint32_t caps) void QWaylandInputDevice::seat_capabilities(uint32_t caps)
{ {
mCaps = caps; mCaps = caps;
if (caps & WL_SEAT_CAPABILITY_KEYBOARD && !mKeyboard) { if (caps & WL_SEAT_CAPABILITY_KEYBOARD && !mKeyboard) {
mKeyboard = createKeyboard(this); mKeyboard.reset(createKeyboard(this));
mKeyboard->init(get_keyboard()); mKeyboard->init(get_keyboard());
} else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && mKeyboard) { } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && mKeyboard) {
delete mKeyboard; mKeyboard.reset();
mKeyboard = nullptr;
} }
if (caps & WL_SEAT_CAPABILITY_POINTER && !mPointer) { if (caps & WL_SEAT_CAPABILITY_POINTER && !mPointer) {
mPointer = createPointer(this); mPointer.reset(createPointer(this));
mPointer->init(get_pointer()); mPointer->init(get_pointer());
auto *pointerGestures = mQDisplay->pointerGestures(); auto *pointerGestures = mQDisplay->pointerGestures();
if (pointerGestures) { if (pointerGestures) {
// NOTE: The name of the device and its system ID are not exposed on Wayland. // NOTE: The name of the device and its system ID are not exposed on Wayland.
mTouchPadDevice = new QPointingDevice(QLatin1String("touchpad"), 0, mTouchPadDevice = new QPointingDevice(
QInputDevice::DeviceType::TouchPad, QLatin1String("touchpad"), 0, QInputDevice::DeviceType::TouchPad,
QPointingDevice::PointerType::Finger, QPointingDevice::PointerType::Finger, QInputDevice::Capability::Position,
QInputDevice::Capability::Position, MaxTouchPoints, 0, QString(), QPointingDeviceUniqueId(), this);
MaxTouchPoints, 0);
QWindowSystemInterface::registerInputDevice(mTouchPadDevice); QWindowSystemInterface::registerInputDevice(mTouchPadDevice);
mPointerGesturePinch = pointerGestures->createPointerGesturePinch(this); mPointerGesturePinch.reset(pointerGestures->createPointerGesturePinch(this));
mPointerGesturePinch->init(pointerGestures->get_pinch_gesture(get_pointer())); mPointerGesturePinch->init(pointerGestures->get_pinch_gesture(get_pointer()));
mPointerGestureSwipe = pointerGestures->createPointerGestureSwipe(this); mPointerGestureSwipe.reset(pointerGestures->createPointerGestureSwipe(this));
mPointerGestureSwipe->init(pointerGestures->get_swipe_gesture(get_pointer())); mPointerGestureSwipe->init(pointerGestures->get_swipe_gesture(get_pointer()));
} }
} else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && mPointer) { } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && mPointer) {
delete mPointer; mPointer.reset();
mPointer = nullptr; mPointerGesturePinch.reset();
delete mPointerGesturePinch; mPointerGestureSwipe.reset();
mPointerGesturePinch = nullptr;
delete mPointerGestureSwipe;
mPointerGestureSwipe = nullptr;
} }
if (caps & WL_SEAT_CAPABILITY_TOUCH && !mTouch) { if (caps & WL_SEAT_CAPABILITY_TOUCH && !mTouch) {
mTouch = createTouch(this); mTouch.reset(createTouch(this));
mTouch->init(get_touch()); mTouch->init(get_touch());
if (!mTouchDevice) { if (!mTouchDevice) {
// TODO number of touchpoints, actual name and ID // TODO number of touchpoints, actual name and ID
mTouchDevice = new QPointingDevice(QLatin1String("some touchscreen"), 0, mTouchDevice = new QPointingDevice(
QInputDevice::DeviceType::TouchScreen, QPointingDevice::PointerType::Finger, QLatin1String("some touchscreen"), 0, QInputDevice::DeviceType::TouchScreen,
QInputDevice::Capability::Position, MaxTouchPoints, 0); QPointingDevice::PointerType::Finger, QInputDevice::Capability::Position,
MaxTouchPoints, 0,QString(), QPointingDeviceUniqueId(), this);
QWindowSystemInterface::registerInputDevice(mTouchDevice); QWindowSystemInterface::registerInputDevice(mTouchDevice);
} }
} else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && mTouch) { } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && mTouch) {
delete mTouch; mTouch.reset();
mTouch = nullptr;
} }
} }
@ -509,27 +500,27 @@ QWaylandInputDevice::Touch *QWaylandInputDevice::createTouch(QWaylandInputDevice
QWaylandInputDevice::Keyboard *QWaylandInputDevice::keyboard() const QWaylandInputDevice::Keyboard *QWaylandInputDevice::keyboard() const
{ {
return mKeyboard; return mKeyboard.data();
} }
QWaylandInputDevice::Pointer *QWaylandInputDevice::pointer() const QWaylandInputDevice::Pointer *QWaylandInputDevice::pointer() const
{ {
return mPointer; return mPointer.data();
} }
QWaylandPointerGestureSwipe *QWaylandInputDevice::pointerGestureSwipe() const QWaylandPointerGestureSwipe *QWaylandInputDevice::pointerGestureSwipe() const
{ {
return mPointerGestureSwipe; return mPointerGestureSwipe.data();
} }
QWaylandPointerGesturePinch *QWaylandInputDevice::pointerGesturePinch() const QWaylandPointerGesturePinch *QWaylandInputDevice::pointerGesturePinch() const
{ {
return mPointerGesturePinch; return mPointerGesturePinch.data();
} }
QWaylandInputDevice::Touch *QWaylandInputDevice::touch() const QWaylandInputDevice::Touch *QWaylandInputDevice::touch() const
{ {
return mTouch; return mTouch.data();
} }
void QWaylandInputDevice::handleEndDrag() void QWaylandInputDevice::handleEndDrag()

View File

@ -193,11 +193,11 @@ protected:
QScopedPointer<QWaylandPrimarySelectionDeviceV1> mPrimarySelectionDevice; QScopedPointer<QWaylandPrimarySelectionDeviceV1> mPrimarySelectionDevice;
#endif #endif
Keyboard *mKeyboard = nullptr; QScopedPointer<Keyboard> mKeyboard;
Pointer *mPointer = nullptr; QScopedPointer<Pointer> mPointer;
QWaylandPointerGestureSwipe *mPointerGestureSwipe = nullptr; QScopedPointer<QWaylandPointerGestureSwipe> mPointerGestureSwipe;
QWaylandPointerGesturePinch *mPointerGesturePinch = nullptr; QScopedPointer<QWaylandPointerGesturePinch> mPointerGesturePinch;
Touch *mTouch = nullptr; QScopedPointer<Touch> mTouch;
QScopedPointer<QWaylandTextInput> mTextInput; QScopedPointer<QWaylandTextInput> mTextInput;
QScopedPointer<QWaylandTextInputMethod> mTextInputMethod; QScopedPointer<QWaylandTextInputMethod> mTextInputMethod;