summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Thiago Macieira <thiago@macieira.org>2013-11-30 09:18:04 -0800
committerGravatar Dirk Hohndel <dirk@hohndel.org>2013-11-30 09:28:42 -0800
commitb22f1da59e37ff4ba871fb2d6c72ee3ec87b4c5b (patch)
tree6548f4639be3261e2db66657b50a6fd095d3a577
parentbd7ded88940995ebbb46fc6983ba9cfd179e55e0 (diff)
downloadsubsurface-b22f1da59e37ff4ba871fb2d6c72ee3ec87b4c5b.tar.gz
Fix all leak-at-exit from singletons in Subsurface
Subsurface creates a lot of singleton instances on demand, but nothing ever deleted them. Since they are singletons, these memory allocations are technically not leaks. However, they clutter the output in valgrind and other memory analysers, hiding the real issues. The solution is to delete these items at exit. For the models and for gettextFromC, the solution is to use a QScopedPointer, which will delete its payload when it gets destroyed. For the dialogs and other widgets, we can't do that: they need to be deleted before QApplication exits, so we just set the parent in all of them to the main window. Signed-off-by: Thiago Macieira <thiago@macieira.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r--gettextfromc.cpp4
-rw-r--r--qt-ui/about.cpp3
-rw-r--r--qt-ui/completionmodels.cpp5
-rw-r--r--qt-ui/divecomputermanagementdialog.cpp2
-rw-r--r--qt-ui/diveplanner.cpp4
-rw-r--r--qt-ui/downloadfromdivecomputer.cpp2
-rw-r--r--qt-ui/models.cpp16
-rw-r--r--qt-ui/preferences.cpp3
-rw-r--r--qt-ui/printdialog.cpp3
-rw-r--r--qt-ui/simplewidgets.cpp8
-rw-r--r--qt-ui/simplewidgets.h4
-rw-r--r--qt-ui/subsurfacewebservices.cpp5
12 files changed, 32 insertions, 27 deletions
diff --git a/gettextfromc.cpp b/gettextfromc.cpp
index fc11847fa..ba8158c87 100644
--- a/gettextfromc.cpp
+++ b/gettextfromc.cpp
@@ -17,8 +17,8 @@ void gettextFromC::reset(void)
gettextFromC* gettextFromC::instance()
{
- static gettextFromC *self = new gettextFromC();
- return self;
+ static QScopedPointer<gettextFromC> self(new gettextFromC());
+ return self.data();
}
extern "C" const char *trGettext(const char *text)
diff --git a/qt-ui/about.cpp b/qt-ui/about.cpp
index 132f496de..dd9431c50 100644
--- a/qt-ui/about.cpp
+++ b/qt-ui/about.cpp
@@ -1,5 +1,6 @@
#include "about.h"
#include "ssrf-version.h"
+#include "mainwindow.h"
#include <QDebug>
#include <QDialogButtonBox>
#include <QNetworkReply>
@@ -7,7 +8,7 @@
SubsurfaceAbout *SubsurfaceAbout::instance()
{
- static SubsurfaceAbout *self = new SubsurfaceAbout();
+ static SubsurfaceAbout *self = new SubsurfaceAbout(mainWindow());
self->setAttribute(Qt::WA_QuitOnClose, false);
return self;
}
diff --git a/qt-ui/completionmodels.cpp b/qt-ui/completionmodels.cpp
index 31733addb..ed1699a92 100644
--- a/qt-ui/completionmodels.cpp
+++ b/qt-ui/completionmodels.cpp
@@ -1,11 +1,12 @@
#include "completionmodels.h"
#include "dive.h"
+#include "mainwindow.h"
#define CREATE_SINGLETON(X) \
X* X::instance() \
{ \
- static X* self = new X(); \
- return self; \
+ static QScopedPointer<X> self(new X()); \
+ return self.data(); \
}
CREATE_SINGLETON(BuddyCompletionModel);
diff --git a/qt-ui/divecomputermanagementdialog.cpp b/qt-ui/divecomputermanagementdialog.cpp
index 883c8e9a7..af27e914f 100644
--- a/qt-ui/divecomputermanagementdialog.cpp
+++ b/qt-ui/divecomputermanagementdialog.cpp
@@ -23,7 +23,7 @@ void DiveComputerManagementDialog::init()
DiveComputerManagementDialog* DiveComputerManagementDialog::instance()
{
- static DiveComputerManagementDialog *self = new DiveComputerManagementDialog();
+ static DiveComputerManagementDialog *self = new DiveComputerManagementDialog(mainWindow());
self->setAttribute(Qt::WA_QuitOnClose, false);
return self;
}
diff --git a/qt-ui/diveplanner.cpp b/qt-ui/diveplanner.cpp
index b18f56bd6..a80ae68ae 100644
--- a/qt-ui/diveplanner.cpp
+++ b/qt-ui/diveplanner.cpp
@@ -1104,8 +1104,8 @@ DivePlannerPointsModel::DivePlannerPointsModel(QObject* parent): QAbstractTableM
DivePlannerPointsModel* DivePlannerPointsModel::instance()
{
- static DivePlannerPointsModel* self = new DivePlannerPointsModel();
- return self;
+ static QScopedPointer<DivePlannerPointsModel> self(new DivePlannerPointsModel());
+ return self.data();
}
void DivePlannerPointsModel::setBottomSac(int sac)
diff --git a/qt-ui/downloadfromdivecomputer.cpp b/qt-ui/downloadfromdivecomputer.cpp
index e7f655203..95582efa3 100644
--- a/qt-ui/downloadfromdivecomputer.cpp
+++ b/qt-ui/downloadfromdivecomputer.cpp
@@ -37,7 +37,7 @@ namespace DownloadFromDcGlobal{
DownloadFromDCWidget *DownloadFromDCWidget::instance()
{
- static DownloadFromDCWidget *dialog = new DownloadFromDCWidget();
+ static DownloadFromDCWidget *dialog = new DownloadFromDCWidget(mainWindow());
dialog->setAttribute(Qt::WA_QuitOnClose, false);
return dialog;
}
diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index 920d362e0..bb0de69f3 100644
--- a/qt-ui/models.cpp
+++ b/qt-ui/models.cpp
@@ -67,8 +67,8 @@ CylindersModel::CylindersModel(QObject* parent): current(0), rows(0)
CylindersModel *CylindersModel::instance()
{
- static CylindersModel *self = new CylindersModel();
- return self;
+ static QScopedPointer<CylindersModel> self(new CylindersModel());
+ return self.data();
}
static QVariant percent_string(fraction_t fraction)
@@ -559,8 +559,8 @@ void WeightModel::setDive(dive* d)
WSInfoModel* WSInfoModel::instance()
{
- static WSInfoModel *self = new WSInfoModel();
- return self;
+ static QScopedPointer<WSInfoModel> self(new WSInfoModel());
+ return self.data();
}
bool WSInfoModel::insertRows(int row, int count, const QModelIndex& parent)
@@ -680,8 +680,8 @@ void WSInfoModel::update()
TankInfoModel* TankInfoModel::instance()
{
- static TankInfoModel *self = new TankInfoModel();
- return self;
+ static QScopedPointer<TankInfoModel> self(new TankInfoModel());
+ return self.data();
}
const QString& TankInfoModel::biggerString() const
@@ -1711,8 +1711,8 @@ Qt::ItemFlags GasSelectionModel::flags(const QModelIndex& index) const
GasSelectionModel* GasSelectionModel::instance()
{
- static GasSelectionModel* self = new GasSelectionModel();
- return self;
+ static QScopedPointer<GasSelectionModel> self(new GasSelectionModel());
+ return self.data();
}
void GasSelectionModel::repopulate()
diff --git a/qt-ui/preferences.cpp b/qt-ui/preferences.cpp
index 4a3189337..5a1a2a5e2 100644
--- a/qt-ui/preferences.cpp
+++ b/qt-ui/preferences.cpp
@@ -1,11 +1,12 @@
#include "preferences.h"
+#include "mainwindow.h"
#include <QSettings>
#include <QDebug>
#include <QFileDialog>
PreferencesDialog* PreferencesDialog::instance()
{
- static PreferencesDialog *dialog = new PreferencesDialog();
+ static PreferencesDialog *dialog = new PreferencesDialog(mainWindow());
dialog->setAttribute(Qt::WA_QuitOnClose, false);
return dialog;
}
diff --git a/qt-ui/printdialog.cpp b/qt-ui/printdialog.cpp
index 76e3a8782..8bf602c34 100644
--- a/qt-ui/printdialog.cpp
+++ b/qt-ui/printdialog.cpp
@@ -1,4 +1,5 @@
#include "printdialog.h"
+#include "mainwindow.h"
#include <QDebug>
#include <QPushButton>
@@ -7,7 +8,7 @@
PrintDialog *PrintDialog::instance()
{
- static PrintDialog *self = new PrintDialog();
+ static PrintDialog *self = new PrintDialog(mainWindow());
self->setAttribute(Qt::WA_QuitOnClose, false);
return self;
}
diff --git a/qt-ui/simplewidgets.cpp b/qt-ui/simplewidgets.cpp
index 006e790bf..4f2b27702 100644
--- a/qt-ui/simplewidgets.cpp
+++ b/qt-ui/simplewidgets.cpp
@@ -106,7 +106,7 @@ void MinMaxAvgWidget::setMinimum(const QString& minimum)
RenumberDialog* RenumberDialog::instance()
{
- static RenumberDialog* self = new RenumberDialog();
+ static RenumberDialog* self = new RenumberDialog(mainWindow());
return self;
}
@@ -118,7 +118,7 @@ void RenumberDialog::buttonClicked(QAbstractButton* button)
}
}
-RenumberDialog::RenumberDialog(): QDialog()
+RenumberDialog::RenumberDialog(QWidget *parent): QDialog(parent)
{
ui.setupUi(this);
connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(buttonClicked(QAbstractButton*)));
@@ -126,7 +126,7 @@ RenumberDialog::RenumberDialog(): QDialog()
ShiftTimesDialog* ShiftTimesDialog::instance()
{
- static ShiftTimesDialog* self = new ShiftTimesDialog();
+ static ShiftTimesDialog* self = new ShiftTimesDialog(mainWindow());
return self;
}
@@ -150,7 +150,7 @@ void ShiftTimesDialog::buttonClicked(QAbstractButton* button)
}
}
-ShiftTimesDialog::ShiftTimesDialog(): QDialog()
+ShiftTimesDialog::ShiftTimesDialog(QWidget *parent): QDialog(parent)
{
ui.setupUi(this);
connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(buttonClicked(QAbstractButton*)));
diff --git a/qt-ui/simplewidgets.h b/qt-ui/simplewidgets.h
index c924370c5..e2ad195e6 100644
--- a/qt-ui/simplewidgets.h
+++ b/qt-ui/simplewidgets.h
@@ -39,7 +39,7 @@ public:
private slots:
void buttonClicked(QAbstractButton *button);
private:
- explicit RenumberDialog();
+ explicit RenumberDialog(QWidget *parent);
Ui::RenumberDialog ui;
};
@@ -50,7 +50,7 @@ public:
private slots:
void buttonClicked(QAbstractButton *button);
private:
- explicit ShiftTimesDialog();
+ explicit ShiftTimesDialog(QWidget *parent);
Ui::ShiftTimesDialog ui;
};
diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp
index e4bfea7a4..1acccda7d 100644
--- a/qt-ui/subsurfacewebservices.cpp
+++ b/qt-ui/subsurfacewebservices.cpp
@@ -1,5 +1,6 @@
#include "subsurfacewebservices.h"
#include "../webservice.h"
+#include "mainwindow.h"
#include <libxml/parser.h>
@@ -44,7 +45,7 @@ void WebServices::hideUpload()
SubsurfaceWebServices* SubsurfaceWebServices::instance()
{
- static SubsurfaceWebServices *self = new SubsurfaceWebServices();
+ static SubsurfaceWebServices *self = new SubsurfaceWebServices(mainWindow());
self->setAttribute(Qt::WA_QuitOnClose, false);
return self;
}
@@ -269,7 +270,7 @@ static bool merge_locations_into_dives(void)
DivelogsDeWebServices* DivelogsDeWebServices::instance()
{
- static DivelogsDeWebServices *self = new DivelogsDeWebServices();
+ static DivelogsDeWebServices *self = new DivelogsDeWebServices(mainWindow());
self->setAttribute(Qt::WA_QuitOnClose, false);
return self;
}