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>
This commit is contained in:
Volker Hilsheimer 2020-05-08 16:23:17 +02:00
parent 973f840e32
commit 6a90078d6b

View File

@ -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: