From db4ffd0b703b43f60b0802a21b434dd5b87f6be4 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Wed, 14 Jan 2015 13:02:18 -0200 Subject: [PATCH] Fix memory leak The QPainter and the QPixmap were being created but never freed. A QPixmap and a QPainter don't need to be created by new, they can be safely created on the stack. So, create them on the stack, pass them via const-reference and use them correctly. Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- qt-ui/profile/divetooltipitem.cpp | 36 +++++++++++++++---------------- qt-ui/profile/divetooltipitem.h | 2 +- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/qt-ui/profile/divetooltipitem.cpp b/qt-ui/profile/divetooltipitem.cpp index 7f7627309..dbef866c5 100644 --- a/qt-ui/profile/divetooltipitem.cpp +++ b/qt-ui/profile/divetooltipitem.cpp @@ -19,7 +19,7 @@ #include "display.h" #endif -void ToolTipItem::addToolTip(const QString &toolTip, const QIcon &icon, const QPixmap *pixmap) +void ToolTipItem::addToolTip(const QString &toolTip, const QIcon &icon, const QPixmap& pixmap) { const IconMetrics& iconMetrics = defaultIconMetrics(); @@ -32,8 +32,8 @@ void ToolTipItem::addToolTip(const QString &toolTip, const QIcon &icon, const QP iconItem = new QGraphicsPixmapItem(icon.pixmap(iconMetrics.sz_small, iconMetrics.sz_small), this); iconItem->setPos(iconMetrics.spacing, yValue); } else { - if (pixmap && !pixmap->isNull()) { - pixmapItem = new QGraphicsPixmapItem(*pixmap, this); + if (!pixmap.isNull()) { + pixmapItem = new QGraphicsPixmapItem(pixmap, this); pixmapItem->setPos(iconMetrics.spacing, yValue); } } @@ -232,8 +232,8 @@ void ToolTipItem::refresh(const QPointF &pos) { int i; struct plot_data *entry; - static QPixmap *tissues = new QPixmap(16,60); - static QPainter *painter = new QPainter(tissues); + static QPixmap tissues(16,60); + static QPainter painter(&tissues); int time = timeAxis->valueAt(pos); if (time == lastTime) return; @@ -244,21 +244,21 @@ void ToolTipItem::refresh(const QPointF &pos) entry = get_plot_details_new(&pInfo, time, &mb); if (entry) { - tissues->fill(); - painter->setPen(QColor(0, 0, 0, 0)); - painter->setBrush(QColor(LIMENADE1)); - painter->drawRect(0, 10 + (100 - AMB_PERCENTAGE) / 2, 16, AMB_PERCENTAGE / 2); - painter->setBrush(QColor(SPRINGWOOD1)); - painter->drawRect(0, 10, 16, (100 - AMB_PERCENTAGE) / 2); - painter->setBrush(QColor("Red")); - painter->drawRect(0,0,16,10); - painter->setPen(QColor(0, 0, 0, 255)); - painter->drawLine(0, 60 - entry->gfline / 2, 16, 60 - entry->gfline / 2); - painter->drawLine(0, 60 - AMB_PERCENTAGE * (entry->pressures.n2 + entry->pressures.he) / entry->ambpressure / 2, + tissues.fill(); + painter.setPen(QColor(0, 0, 0, 0)); + painter.setBrush(QColor(LIMENADE1)); + painter.drawRect(0, 10 + (100 - AMB_PERCENTAGE) / 2, 16, AMB_PERCENTAGE / 2); + painter.setBrush(QColor(SPRINGWOOD1)); + painter.drawRect(0, 10, 16, (100 - AMB_PERCENTAGE) / 2); + painter.setBrush(QColor(Qt::red)); + painter.drawRect(0,0,16,10); + painter.setPen(QColor(0, 0, 0, 255)); + painter.drawLine(0, 60 - entry->gfline / 2, 16, 60 - entry->gfline / 2); + painter.drawLine(0, 60 - AMB_PERCENTAGE * (entry->pressures.n2 + entry->pressures.he) / entry->ambpressure / 2, 16, 60 - AMB_PERCENTAGE * (entry->pressures.n2 + entry->pressures.he) / entry->ambpressure /2); - painter->setPen(QColor(0, 0, 0, 127)); + painter.setPen(QColor(0, 0, 0, 127)); for (i=0; i<16; i++) { - painter->drawLine(i, 60, i, 60 - entry->percentages[i] / 2); + painter.drawLine(i, 60, i, 60 - entry->percentages[i] / 2); } addToolTip(QString::fromUtf8(mb.buffer, mb.len),QIcon(), tissues); } diff --git a/qt-ui/profile/divetooltipitem.h b/qt-ui/profile/divetooltipitem.h index 51e8eccd1..bb3ad84d3 100644 --- a/qt-ui/profile/divetooltipitem.h +++ b/qt-ui/profile/divetooltipitem.h @@ -34,7 +34,7 @@ public: void collapse(); void expand(); void clear(); - void addToolTip(const QString &toolTip, const QIcon &icon = QIcon(), const QPixmap *pixmap = NULL); + void addToolTip(const QString &toolTip, const QIcon &icon = QIcon(), const QPixmap &pixmap = QPixmap()); void refresh(const QPointF &pos); bool isExpanded() const; void persistPos();