From 003b2c2e44118717ed05b5fb88b49a0260ee07ed Mon Sep 17 00:00:00 2001
From: Dmitri Naumov <github@naumov.de>
Date: Sun, 16 Apr 2017 15:03:45 +0200
Subject: [PATCH] [MeL] Avoid memory leak.

In case of no match for the scanned element types
the allocated memory is automatically released.
---
 MeshLib/MeshEditing/AddLayerToMesh.cpp | 10 ++++++----
 MeshLib/MeshEditing/FlipElements.cpp   | 22 +++++++++++++---------
 2 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/MeshLib/MeshEditing/AddLayerToMesh.cpp b/MeshLib/MeshEditing/AddLayerToMesh.cpp
index 13e7454f3d7..d518a7335e9 100644
--- a/MeshLib/MeshEditing/AddLayerToMesh.cpp
+++ b/MeshLib/MeshEditing/AddLayerToMesh.cpp
@@ -53,7 +53,9 @@ MeshLib::Element* extrudeElement(std::vector<MeshLib::Node*> const& subsfc_nodes
         return nullptr;
 
     const unsigned nElemNodes(sfc_elem.getNumberOfBaseNodes());
-    auto** new_nodes = new MeshLib::Node*[2 * nElemNodes];
+    auto new_nodes = std::unique_ptr<MeshLib::Node* []> {
+        new MeshLib::Node*[2 * nElemNodes]
+    };
 
     for (unsigned j=0; j<nElemNodes; ++j)
     {
@@ -65,11 +67,11 @@ MeshLib::Element* extrudeElement(std::vector<MeshLib::Node*> const& subsfc_nodes
     }
 
     if (sfc_elem.getGeomType() == MeshLib::MeshElemType::LINE)
-        return new MeshLib::Quad(new_nodes);
+        return new MeshLib::Quad(new_nodes.release());
     if (sfc_elem.getGeomType() == MeshLib::MeshElemType::TRIANGLE)
-        return new MeshLib::Prism(new_nodes);
+        return new MeshLib::Prism(new_nodes.release());
     if (sfc_elem.getGeomType() == MeshLib::MeshElemType::QUAD)
-        return new MeshLib::Hex(new_nodes);
+        return new MeshLib::Hex(new_nodes.release());
 
     return nullptr;
 }
diff --git a/MeshLib/MeshEditing/FlipElements.cpp b/MeshLib/MeshEditing/FlipElements.cpp
index 4cac837513f..3a58eed0df3 100644
--- a/MeshLib/MeshEditing/FlipElements.cpp
+++ b/MeshLib/MeshEditing/FlipElements.cpp
@@ -18,26 +18,30 @@
 
 namespace MeshLib
 {
-
-std::unique_ptr<MeshLib::Element> createFlippedElement(MeshLib::Element const& elem, std::vector<MeshLib::Node*> const& nodes)
+std::unique_ptr<MeshLib::Element> createFlippedElement(
+    MeshLib::Element const& elem, std::vector<MeshLib::Node*> const& nodes)
 {
-    if (elem.getDimension()>2)
+    if (elem.getDimension() > 2)
         return nullptr;
 
-    unsigned const n_nodes (elem.getNumberOfNodes());
-    auto** elem_nodes = new MeshLib::Node*[n_nodes];
-    for (unsigned i=0; i<n_nodes; ++i)
+    unsigned const n_nodes(elem.getNumberOfNodes());
+    auto elem_nodes =
+        std::unique_ptr<MeshLib::Node* []> { new MeshLib::Node*[n_nodes] };
+    for (unsigned i = 0; i < n_nodes; ++i)
         elem_nodes[i] = nodes[elem.getNode(i)->getID()];
     std::swap(elem_nodes[0], elem_nodes[1]);
 
     if (elem.getGeomType() == MeshElemType::LINE)
-        return std::unique_ptr<MeshLib::Line>(new MeshLib::Line(elem_nodes, elem.getID()));
+        return std::unique_ptr<MeshLib::Line>(
+            new MeshLib::Line(elem_nodes.release(), elem.getID()));
     else if (elem.getGeomType() == MeshElemType::TRIANGLE)
-        return std::unique_ptr<MeshLib::Tri>(new MeshLib::Tri(elem_nodes, elem.getID()));
+        return std::unique_ptr<MeshLib::Tri>(
+            new MeshLib::Tri(elem_nodes.release(), elem.getID()));
     else if (elem.getGeomType() == MeshElemType::QUAD)
     {
         std::swap(elem_nodes[2], elem_nodes[3]);
-        return std::unique_ptr<MeshLib::Quad>(new MeshLib::Quad(elem_nodes, elem.getID()));
+        return std::unique_ptr<MeshLib::Quad>(
+            new MeshLib::Quad(elem_nodes.release(), elem.getID()));
     }
     return nullptr;
 }
-- 
GitLab