From f46bfe71414283115d7e8062dbce79bf5aa510ad Mon Sep 17 00:00:00 2001
From: Antti Soininen <soininen@ill.fr>
Date: Tue, 26 Sep 2017 15:24:52 +0200
Subject: [PATCH] Use unique ptr in matrix tab extension. Plugs a memory leak.

Re #20632
---
 MantidPlot/src/Mantid/MantidMatrix.cpp        | 19 ++++++++-----------
 .../Mantid/MantidMatrixDxExtensionHandler.cpp |  6 +++---
 .../Mantid/MantidMatrixExtensionRequest.cpp   |  4 ++--
 .../src/Mantid/MantidMatrixTabExtension.h     |  4 ++--
 4 files changed, 15 insertions(+), 18 deletions(-)

diff --git a/MantidPlot/src/Mantid/MantidMatrix.cpp b/MantidPlot/src/Mantid/MantidMatrix.cpp
index 577746f4aa5..a52fa2b12ef 100644
--- a/MantidPlot/src/Mantid/MantidMatrix.cpp
+++ b/MantidPlot/src/Mantid/MantidMatrix.cpp
@@ -1292,21 +1292,19 @@ void MantidMatrix::setupNewExtension(MantidMatrixModel::Type type) {
   // We need to hook up the extension
   extension.model = new MantidMatrixModel(this, m_workspace.get(), m_rows,
                                           m_cols, m_startRow, type);
-  extension.tableView = new QTableView();
-
-  // Add it to the extension collection, so we can set it up in place
-  m_extensions.emplace(type, extension);
-  auto mapped_extension = m_extensions[type];
+  extension.tableView = Mantid::Kernel::make_unique<QTableView>();
 
   // Add a new tab
-  m_tabs->insertTab(modelTypeToInt(type), mapped_extension.tableView,
-                    mapped_extension.label);
+  m_tabs->insertTab(modelTypeToInt(type), extension.tableView.get(),
+                    extension.label);
 
   // Install the eventfilter
-  mapped_extension.tableView->installEventFilter(this);
+  extension.tableView->installEventFilter(this);
 
   // Connect Table View
-  connectTableView(mapped_extension.tableView, mapped_extension.model);
+  connectTableView(extension.tableView.get(), extension.model);
+
+  m_extensions.emplace(type, std::move(extension));
 
   // Set the column width
   auto columnWidth = m_extensionRequest.getColumnWidthPreference(
@@ -1334,12 +1332,11 @@ void MantidMatrix::updateExtensions(Mantid::API::MatrixWorkspace_sptr ws) {
         auto &extension = it->second;
         extension.model = new MantidMatrixModel(this, ws.get(), m_rows, m_cols,
                                                 m_startRow, it->first);
-        connectTableView(extension.tableView, extension.model);
+        connectTableView(extension.tableView.get(), extension.model);
         ++it;
       } else {
         closeDependants();
         m_tabs->removeTab(modelTypeToInt(it->first));
-        delete it->second.tableView;
         it = m_extensions.erase(it);
       }
       break;
diff --git a/MantidPlot/src/Mantid/MantidMatrixDxExtensionHandler.cpp b/MantidPlot/src/Mantid/MantidMatrixDxExtensionHandler.cpp
index b261b76f956..99aa9c10178 100644
--- a/MantidPlot/src/Mantid/MantidMatrixDxExtensionHandler.cpp
+++ b/MantidPlot/src/Mantid/MantidMatrixDxExtensionHandler.cpp
@@ -76,7 +76,7 @@ int MantidMatrixDxExtensionHandler::getPrecision(
 void MantidMatrixDxExtensionHandler::setColumnWidth(
     MantidMatrixTabExtension &extension, int width, int numberOfColumns) {
   if (extension.type == m_type) {
-    auto table_view = extension.tableView;
+    auto &table_view = extension.tableView;
     table_view->horizontalHeader()->setDefaultSectionSize(width);
     for (int i = 0; i < numberOfColumns; i++) {
       table_view->setColumnWidth(i, width);
@@ -94,7 +94,7 @@ void MantidMatrixDxExtensionHandler::setColumnWidth(
 int MantidMatrixDxExtensionHandler::getColumnWidth(
     MantidMatrixTabExtension &extension) {
   if (extension.type == m_type) {
-    auto table_view = extension.tableView;
+    auto &table_view = extension.tableView;
     return table_view->columnWidth(0);
   } else {
     return m_successor->getColumnWidth(extension);
@@ -109,7 +109,7 @@ int MantidMatrixDxExtensionHandler::getColumnWidth(
 QTableView *MantidMatrixDxExtensionHandler::getTableView(
     MantidMatrixTabExtension &extension) {
   if (extension.type == m_type) {
-    return extension.tableView;
+    return extension.tableView.get();
   } else {
     return m_successor->getTableView(extension);
   }
diff --git a/MantidPlot/src/Mantid/MantidMatrixExtensionRequest.cpp b/MantidPlot/src/Mantid/MantidMatrixExtensionRequest.cpp
index 799d1abb246..01b297c9b73 100644
--- a/MantidPlot/src/Mantid/MantidMatrixExtensionRequest.cpp
+++ b/MantidPlot/src/Mantid/MantidMatrixExtensionRequest.cpp
@@ -215,7 +215,7 @@ bool MantidMatrixExtensionRequest::tableViewMatchesObject(
     MantidMatrixTabExtensionMap &extensions, QObject *object) {
   for (auto it = extensions.begin(); it != extensions.end(); ++it) {
     auto &extension = it->second;
-    if (extension.tableView == object) {
+    if (extension.tableView.get() == object) {
       return true;
     }
   }
@@ -234,7 +234,7 @@ QTableView *MantidMatrixExtensionRequest::getActiveView(
     QTableView *defaultValue) {
   if (extensions.count(type) > 0) {
     auto &extension = extensions[type];
-    return extension.tableView;
+    return extension.tableView.get();
   } else {
     return defaultValue;
   }
diff --git a/MantidPlot/src/Mantid/MantidMatrixTabExtension.h b/MantidPlot/src/Mantid/MantidMatrixTabExtension.h
index bc91ba0bf59..81110935344 100644
--- a/MantidPlot/src/Mantid/MantidMatrixTabExtension.h
+++ b/MantidPlot/src/Mantid/MantidMatrixTabExtension.h
@@ -15,10 +15,10 @@ struct MantidMatrixTabExtension {
                            MantidMatrixModel::Type type)
       : label(label), tableView(tableView), model(model), type(type) {}
   MantidMatrixTabExtension()
-      : label(""), tableView(NULL), model(NULL),
+      : label(""), tableView(), model(NULL),
         type(MantidMatrixModel::Type::DX) {}
   QString label;
-  QTableView *tableView;
+  std::unique_ptr<QTableView> tableView;
   QPointer<MantidMatrixModel> model;
   MantidMatrixModel::Type type;
 };
-- 
GitLab