summaryrefslogtreecommitdiffstats
path: root/src/widgets/graphicsview
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2020-05-08 16:23:17 +0200
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2020-05-12 12:52:52 +0200
commit6a90078d6bfe7900b6c9a6ea8c758172c7bc241f (patch)
treed8078dae34c8e308133b602b7b085c518f1c7aff /src/widgets/graphicsview
parent973f840e323e5405cbf166de085743c9c8dc59dd (diff)
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 <edward.welbourne@qt.io> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/widgets/graphicsview')
-rw-r--r--src/widgets/graphicsview/qgraphicsanchorlayout_p.h35
1 files changed, 18 insertions, 17 deletions
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<AnchorData *> m_firstAnchors;
QList<AnchorData *> 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<const AnchorVertexPair *>(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: