From 14302ddbac1e6d9c95de15c97362c3de09f545eb Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 7 Oct 2016 16:20:20 +0900 Subject: Fixed massive leaks of Event objects --- jobs/syncjob.h | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'jobs/syncjob.h') diff --git a/jobs/syncjob.h b/jobs/syncjob.h index ed99b38b..55d7458c 100644 --- a/jobs/syncjob.h +++ b/jobs/syncjob.h @@ -26,10 +26,34 @@ namespace QMatrixClient { + /** + * @brief A crude wrapper around a container of pointers that owns pointers + * to contained objects + * + * Similar to vector>, upon deletion, EventsHolder + * will delete all events contained in it. + */ + template + class Owning : public ContainerT + { + public: + Owning() = default; + Owning(Owning&) = delete; + Owning(Owning&& other) : ContainerT(std::move(other)) { } + ~Owning() { for (auto e: *this) delete e; } + + /** + * @brief returns the underlying events and releases the ownership + * + * Acts similar to unique_ptr::release. + */ + ContainerT release() { return std::move(*this); } + }; + class SyncRoomData { public: - class EventList : public Events + class EventList : public Owning { private: QString jsonKey; @@ -55,7 +79,13 @@ namespace QMatrixClient JoinState joinState_ = JoinState::Join, const QJsonObject& room_ = QJsonObject()); }; - using SyncData = QVector; +} +Q_DECLARE_TYPEINFO(QMatrixClient::SyncRoomData, Q_MOVABLE_TYPE); + +namespace QMatrixClient +{ + // QVector cannot work with non-copiable objects, std::vector can. + using SyncData = std::vector; class ConnectionData; class SyncJob: public BaseJob @@ -69,7 +99,7 @@ namespace QMatrixClient void setPresence(QString presence); void setTimeout(int timeout); - const SyncData& roomData() const; + SyncData& roomData(); QString nextBatch() const; protected: @@ -82,6 +112,5 @@ namespace QMatrixClient Private* d; }; } -Q_DECLARE_TYPEINFO(QMatrixClient::SyncRoomData, Q_MOVABLE_TYPE); #endif // QMATRIXCLIENT_SYNCJOB_H -- cgit v1.2.3 From 29bff90ecb0d1febfa8728383195f0f41c9a29ef Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 7 Oct 2016 18:55:07 +0900 Subject: Fix building with VS2013 --- jobs/syncjob.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'jobs/syncjob.h') diff --git a/jobs/syncjob.h b/jobs/syncjob.h index 55d7458c..a9138a65 100644 --- a/jobs/syncjob.h +++ b/jobs/syncjob.h @@ -38,7 +38,15 @@ namespace QMatrixClient { public: Owning() = default; +#if defined(_MSC_VER) && _MSC_VER < 1900 + // Workaround: Dangerous (auto_ptr style) copy constructor because + // VS2013 (unnecessarily) instantiates EventList::QVector<>::toList() + // which instantiates QList< Owning<> > which needs the contained + // object to have a non-deleted copy constructor. + Owning(Owning& other) : ContainerT(std::move(other)) { } +#else Owning(Owning&) = delete; +#endif Owning(Owning&& other) : ContainerT(std::move(other)) { } ~Owning() { for (auto e: *this) delete e; } -- cgit v1.2.3