From 6a90078d6bfe7900b6c9a6ea8c758172c7bc241f Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Fri, 8 May 2020 16:23:17 +0200 Subject: Fix potential memory leak by adding a virtual destructor to AnchorVertex The subclass AnchorVertexPair is allocated and passed around as pointers to AnchorVertex, and placed in lists that are then later cleaned up via qDeleteAll. This very likely results in memory leaks, as the compiler- generated ~AnchorVertexPair destructor is never called. Add a virtual destructor. Since there now is a vtable generated for AnchorVertex, remove the m_type member (which is only used for string generation in debug builds) and make toString virtual instead. Change-Id: I2cf184c0b1da1bd59b056a0f696a0e5479d4cb4e Fixes: QTBUG-84094 Coverity-Id: 218707 Pick-to: 5.15 Reviewed-by: Edward Welbourne Reviewed-by: Lars Knoll --- src/widgets/graphicsview/qgraphicsanchorlayout_p.h | 35 +++++++++++----------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'src/widgets/graphicsview') diff --git a/src/widgets/graphicsview/qgraphicsanchorlayout_p.h b/src/widgets/graphicsview/qgraphicsanchorlayout_p.h index 25f7379517..4f746f0172 100644 --- a/src/widgets/graphicsview/qgraphicsanchorlayout_p.h +++ b/src/widgets/graphicsview/qgraphicsanchorlayout_p.h @@ -83,25 +83,22 @@ namespace QtGraphicsAnchorLayout { Represents a vertex (anchorage point) in the internal graph */ -struct AnchorVertex { - enum Type { - Normal = 0, - Pair - }; - +struct AnchorVertex +{ AnchorVertex(QGraphicsLayoutItem *item, Qt::AnchorPoint edge) - : m_item(item), m_edge(edge), m_type(Normal) {} + : m_item(item), m_edge(edge) {} AnchorVertex() - : m_item(nullptr), m_edge(Qt::AnchorPoint(0)), m_type(Normal) {} + : m_item(nullptr), m_edge(Qt::AnchorPoint(0)) {} + + virtual ~AnchorVertex() = default; #ifdef QT_DEBUG - inline QString toString() const; + virtual inline QString toString() const; #endif QGraphicsLayoutItem *m_item; Qt::AnchorPoint m_edge; - uint m_type : 1; // Current distance from this vertex to the layout edge (Left or Top) // Value is calculated from the current anchors sizes. @@ -250,8 +247,8 @@ struct ParallelAnchorData : public AnchorData struct AnchorVertexPair : public AnchorVertex { AnchorVertexPair(AnchorVertex *v1, AnchorVertex *v2, AnchorData *data) - : AnchorVertex(), m_first(v1), m_second(v2), m_removedAnchor(data) { - m_type = AnchorVertex::Pair; + : AnchorVertex(), m_first(v1), m_second(v2), m_removedAnchor(data) + { } AnchorVertex *m_first; @@ -260,17 +257,21 @@ struct AnchorVertexPair : public AnchorVertex { AnchorData *m_removedAnchor; QList m_firstAnchors; QList m_secondAnchors; + +#ifdef QT_DEBUG + inline QString toString() const override + { + return QString::fromLatin1("(%1, %2)").arg(m_first->toString(), m_second->toString()); + } +#endif }; #ifdef QT_DEBUG inline QString AnchorVertex::toString() const { - if (m_type == Pair) { - const AnchorVertexPair *vp = static_cast(this); - return QString::fromLatin1("(%1, %2)").arg(vp->m_first->toString(), vp->m_second->toString()); - } else if (!m_item) { + if (!m_item) return QString::fromLatin1("NULL_%1").arg(quintptr(this)); - } + QString edge; switch (m_edge) { case Qt::AnchorLeft: -- cgit v1.2.3