Commit 05ad24cc authored by David Jarvie's avatar David Jarvie
Browse files

Fix memory leak when objects disconnect from DailyTimer

parent 3a81c483
Pipeline #36261 failed with stage
in 6 minutes and 50 seconds
/*
* synchtimer.cpp - timers which synchronize to time boundaries
* Program: kalarm
* Copyright © 2004,2005,2007-2009 David Jarvie <djarvie@kde.org>
* Copyright © 2004-2020 David Jarvie <djarvie@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -53,6 +53,7 @@ void SynchTimer::connecT(QObject* receiver, const char* member)
return; // the slot is already connected, so ignore request
connect(mTimer, SIGNAL(timeout()), receiver, member);
mConnections.append(connection);
connect(receiver, &QObject::destroyed, this, &SynchTimer::slotReceiverGone);
if (!mTimer->isActive())
{
connect(mTimer, &QTimer::timeout, this, &SynchTimer::slotTimer);
......@@ -157,11 +158,18 @@ DailyTimer* DailyTimer::fixedInstance(const QTime& timeOfDay, bool create)
void DailyTimer::disconnect(const QTime& timeOfDay, QObject* receiver, const char* member)
{
DailyTimer* timer = fixedInstance(timeOfDay, false);
if (!timer)
return;
timer->disconnecT(receiver, member);
if (!timer->hasConnections())
delete timer;
if (timer)
timer->disconnecT(receiver, member);
}
/******************************************************************************
* Disconnect from the timer. The timer is deleted if no longer used.
*/
void DailyTimer::disconnecT(QObject* receiver, const char* member)
{
SynchTimer::disconnecT(receiver, member);
if (!hasConnections())
deleteLater();
}
/******************************************************************************
......
/*
* synchtimer.h - timers which synchronize to time boundaries
* Program: kalarm
* Copyright © 2004,2005 by David Jarvie <djarvie@kde.org>
* Copyright © 2004-2020 by David Jarvie <djarvie@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -53,7 +53,7 @@ class SynchTimer : public QObject
SynchTimer();
virtual void start() = 0;
void connecT(QObject* receiver, const char* member);
void disconnecT(QObject* receiver, const char* member = nullptr);
virtual void disconnecT(QObject* receiver, const char* member = nullptr);
bool hasConnections() const { return !mConnections.isEmpty(); }
QTimer* mTimer;
......@@ -142,6 +142,8 @@ class DailyTimer : public SynchTimer
*/
DailyTimer(const QTime&, bool fixed);
void disconnecT(QObject* receiver, const char* member = nullptr) override;
/** Change the time at which this variable timer triggers.
* @param newTimeOfDay New time at which the timer should trigger.
* @param triggerMissed If true, and if @p newTimeOfDay < @p oldTimeOfDay, and if the current
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment