Fix memory leak in download-from-dive-computer widget

Instead of (re)allocating the vendor and product models, use the
setStringList method on sub objects.

Even though only a theoretical problem, the model objects are moved
in front of the ui object, so that the widgets referencing the models
are destroyed first.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2017-11-11 19:40:47 +01:00 committed by Lubomir I. Ivanov
parent 35c5016777
commit efabf0a55c
2 changed files with 8 additions and 17 deletions

View file

@ -24,8 +24,6 @@ namespace DownloadFromDcGlobal {
DownloadFromDCWidget::DownloadFromDCWidget(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f), DownloadFromDCWidget::DownloadFromDCWidget(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f),
downloading(false), downloading(false),
previousLast(0), previousLast(0),
vendorModel(0),
productModel(0),
timer(new QTimer(this)), timer(new QTimer(this)),
dumpWarningShown(false), dumpWarningShown(false),
ostcFirmwareCheck(0), ostcFirmwareCheck(0),
@ -33,7 +31,7 @@ DownloadFromDCWidget::DownloadFromDCWidget(QWidget *parent, Qt::WindowFlags f) :
{ {
diveImportedModel = new DiveImportedModel(this); diveImportedModel = new DiveImportedModel(this);
diveImportedModel->setDiveTable(&downloadTable); diveImportedModel->setDiveTable(&downloadTable);
vendorModel = new QStringListModel(vendorList); vendorModel.setStringList(vendorList);
QShortcut *close = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this); QShortcut *close = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this); QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
@ -54,7 +52,8 @@ DownloadFromDCWidget::DownloadFromDCWidget(QWidget *parent, Qt::WindowFlags f) :
ui.chooseLogFile->setEnabled(ui.logToFile->isChecked()); ui.chooseLogFile->setEnabled(ui.logToFile->isChecked());
ui.selectAllButton->setEnabled(false); ui.selectAllButton->setEnabled(false);
ui.unselectAllButton->setEnabled(false); ui.unselectAllButton->setEnabled(false);
ui.vendor->setModel(vendorModel); ui.vendor->setModel(&vendorModel);
ui.product->setModel(&productModel);
progress_bar_text = ""; progress_bar_text = "";
@ -81,8 +80,7 @@ DownloadFromDCWidget::DownloadFromDCWidget(QWidget *parent, Qt::WindowFlags f) :
auto dc = SettingsObjectWrapper::instance()->dive_computer_settings; auto dc = SettingsObjectWrapper::instance()->dive_computer_settings;
if (!dc->dc_vendor().isEmpty()) { if (!dc->dc_vendor().isEmpty()) {
ui.vendor->setCurrentIndex(ui.vendor->findText(dc->dc_vendor())); ui.vendor->setCurrentIndex(ui.vendor->findText(dc->dc_vendor()));
productModel = new QStringListModel(productList[dc->dc_vendor()]); productModel.setStringList(productList[dc->dc_vendor()]);
ui.product->setModel(productModel);
if (!dc->dc_product().isEmpty()) if (!dc->dc_product().isEmpty())
ui.product->setCurrentIndex(ui.product->findText(dc->dc_product())); ui.product->setCurrentIndex(ui.product->findText(dc->dc_product()));
} }
@ -241,19 +239,12 @@ void DownloadFromDCWidget::updateState(states state)
void DownloadFromDCWidget::on_vendor_currentIndexChanged(const QString &vendor) void DownloadFromDCWidget::on_vendor_currentIndexChanged(const QString &vendor)
{ {
int dcType = DC_TYPE_SERIAL; int dcType = DC_TYPE_SERIAL;
QAbstractItemModel *currentModel = ui.product->model(); productModel.setStringList(productList[vendor]);
if (!currentModel) ui.product->setCurrentIndex(0);
return;
productModel = new QStringListModel(productList[vendor]);
ui.product->setModel(productModel);
if (vendor == QString("Uemis")) if (vendor == QString("Uemis"))
dcType = DC_TYPE_UEMIS; dcType = DC_TYPE_UEMIS;
fill_device_list(dcType); fill_device_list(dcType);
// Memleak - but deleting gives me a crash.
//currentModel->deleteLater();
} }
void DownloadFromDCWidget::on_product_currentIndexChanged(const QString &product) void DownloadFromDCWidget::on_product_currentIndexChanged(const QString &product)

View file

@ -60,14 +60,14 @@ private:
void markChildrenAsEnabled(); void markChildrenAsEnabled();
void updateDeviceEnabled(); void updateDeviceEnabled();
QStringListModel vendorModel;
QStringListModel productModel;
Ui::DownloadFromDiveComputer ui; Ui::DownloadFromDiveComputer ui;
DownloadThread thread; DownloadThread thread;
bool downloading; bool downloading;
int previousLast; int previousLast;
QStringListModel *vendorModel;
QStringListModel *productModel;
void fill_device_list(int dc_type); void fill_device_list(int dc_type);
QTimer *timer; QTimer *timer;
bool dumpWarningShown; bool dumpWarningShown;