Commit 6c6f15b6 authored by Dave Moxey's avatar Dave Moxey

Merge branch 'fix/shared_ptr_mem_leak' into 'master'

Fix for memory leaks in NekMesh

See merge request nektar/nektar!1104
parents 49b288f4 afa65b18
......@@ -83,6 +83,7 @@ typedef std::shared_ptr<NekMatrix<NekDouble> > SharedMatrix;
class NodalUtil
{
public:
LIB_UTILITIES_EXPORT virtual ~NodalUtil() = default;
LIB_UTILITIES_EXPORT NekVector<NekDouble> GetWeights();
LIB_UTILITIES_EXPORT SharedMatrix GetVandermonde();
LIB_UTILITIES_EXPORT SharedMatrix GetVandermondeForDeriv(int dir);
......
......@@ -90,9 +90,8 @@ namespace Nektar
~ThreadSpecificPool()
{
// The documentation isn't particularly clear if delete needs to be called manually
// or if the thread specific pointer will call delete for me. Looking through the
// boost code doesn't make it any clearer.
// Need to call delete manually, otherwise memory is leaking.
delete m_pool;
}
/// \brief Allocate a block of memory of size ByteSize.
......
......@@ -49,13 +49,13 @@ Array<OneD, NekDouble> CADCurve::NormalWRT(NekDouble t, int surf)
boost::ignore_unused(surf);
Array<OneD, NekDouble> p = P(t);
pair<CADSurfSharedPtr, CADOrientation::Orientation> surface;
pair<weak_ptr<CADSurf>, CADOrientation::Orientation> surface;
ASSERTL0(m_adjSurfs.size() == 1,
"This will only work in 2D for one surface at the moment");
surface = m_adjSurfs[0];
Array<OneD, NekDouble> uv = surface.first->locuv(p);
Array<OneD, NekDouble> d1 = surface.first->D1(uv);
Array<OneD, NekDouble> uv = surface.first.lock()->locuv(p);
Array<OneD, NekDouble> d1 = surface.first.lock()->D1(uv);
NekDouble t1 = t - 1e-8;
NekDouble t2 = t + 1e-8;
......@@ -65,8 +65,8 @@ Array<OneD, NekDouble> CADCurve::NormalWRT(NekDouble t, int surf)
swap(t1, t2);
}
Array<OneD, NekDouble> uv1 = surface.first->locuv(P(t1));
Array<OneD, NekDouble> uv2 = surface.first->locuv(P(t2));
Array<OneD, NekDouble> uv1 = surface.first.lock()->locuv(P(t1));
Array<OneD, NekDouble> uv2 = surface.first.lock()->locuv(P(t2));
NekDouble du = uv2[1] - uv1[1];
NekDouble dv = -1.0 * (uv2[0] - uv1[0]);
......@@ -88,7 +88,7 @@ CADOrientation::Orientation CADCurve::GetOrienationWRT(int surf)
{
for (int i = 0; i < m_adjSurfs.size(); i++)
{
if (m_adjSurfs[i].first->GetId() == surf)
if (m_adjSurfs[i].first.lock()->GetId() == surf)
{
return m_adjSurfs[i].second;
}
......
......@@ -154,7 +154,7 @@ public:
* @brief returns the ids of surfaces bound by this curve as well as their
* Orientation with respect to the loop of curves
*/
std::vector<std::pair<CADSurfSharedPtr, CADOrientation::Orientation>>
std::vector<std::pair<std::weak_ptr<CADSurf>, CADOrientation::Orientation>>
GetAdjSurf()
{
return m_adjSurfs;
......@@ -214,7 +214,7 @@ protected:
/// Length of edge
NekDouble m_length;
/// List of surfaces which this curve belongs to.
std::vector<std::pair<CADSurfSharedPtr, CADOrientation::Orientation>>
std::vector<std::pair<std::weak_ptr<CADSurf>, CADOrientation::Orientation>>
m_adjSurfs;
/// list of end vertices
std::vector<CADVertSharedPtr> m_mainVerts;
......
......@@ -32,10 +32,7 @@
//
////////////////////////////////////////////////////////////////////////////////
#include <NekMeshUtils/CADSystem/CADCurve.h>
#include <NekMeshUtils/CADSystem/CADSurf.h>
#include <NekMeshUtils/CADSystem/CADSystem.h>
#include <NekMeshUtils/CADSystem/CADVert.h>
using namespace std;
......
......@@ -42,6 +42,10 @@
#include <NekMeshUtils/NekMeshUtilsDeclspec.h>
#include <NekMeshUtils/CADSystem/CADVert.h>
#include <NekMeshUtils/CADSystem/CADCurve.h>
#include <NekMeshUtils/CADSystem/CADSurf.h>
#include "CADObject.h"
namespace Nektar
......
......@@ -32,7 +32,6 @@
//
////////////////////////////////////////////////////////////////////////////////
#include "CADVert.h"
#include <NekMeshUtils/MeshElements/Node.h>
using namespace std;
......
......@@ -40,6 +40,8 @@
#include <NekMeshUtils/CADSystem/CADObject.h>
#include <NekMeshUtils/MeshElements/Node.h>
namespace Nektar
{
namespace NekMeshUtils
......@@ -118,7 +120,7 @@ public:
/**
* @brief Get list of CAD curves which are bound by this vertex
*/
std::vector<CADCurveSharedPtr> GetAdjCurves()
std::vector<std::weak_ptr<CADCurve> > GetAdjCurves()
{
return curves;
}
......@@ -131,7 +133,7 @@ protected:
/// degen surface
int degensurf;
/// adjacent curves
std::vector<CADCurveSharedPtr> curves;
std::vector<std::weak_ptr<CADCurve> > curves;
};
typedef std::shared_ptr<CADVert> CADVertSharedPtr;
......
......@@ -36,7 +36,11 @@
#define NEKMESHUTILS_CADSYSTEM_CFI_CADCURVECFI
#include "../CADCurve.h"
#include "CADSystemCFI.h"
#ifndef NEK_CADFIXAPI_HXX
#define NEK_CADFIXAPI_HXX
#include "cadfixapi.hxx"
#endif
namespace Nektar
{
......
......@@ -35,9 +35,13 @@
#ifndef NEKMESHUTILS_CADSYSTEM_CFI_CADELEMENTCFI
#define NEKMESHUTILS_CADSYSTEM_CFI_CADELEMENTCFI
#include "CADSystemCFI.h"
#include "../CADObject.h"
#ifndef NEK_CADFIXAPI_HXX
#define NEK_CADFIXAPI_HXX
#include "cadfixapi.hxx"
#endif
namespace Nektar
{
namespace NekMeshUtils
......
......@@ -36,8 +36,11 @@
#define NEKMESHUTILS_CADSYSTEM_CFI_CADSURFCFI
#include "../CADSurf.h"
#include "CADSystemCFI.h"
#ifndef NEK_CADFIXAPI_HXX
#define NEK_CADFIXAPI_HXX
#include "cadfixapi.hxx"
#endif
namespace Nektar
{
namespace NekMeshUtils
......
......@@ -215,6 +215,8 @@ bool CADSystemCFI::LoadCAD()
}
}
delete edgeList;
for (it2 = fullEdgeList.begin(); it2 != fullEdgeList.end(); it2++)
{
cfi::Oriented<cfi::TopoEntity *> orientatedEdge = *it2;
......@@ -233,11 +235,13 @@ bool CADSystemCFI::LoadCAD()
m_mapVertToListEdge[vert->getName()].push_back(
edge->getName());
}
delete vertList;
}
mapOfFaces[face->getName()] = face;
}
}
delete faceList;
}
// make the vertices and build a map of name to id
......@@ -286,6 +290,9 @@ bool CADSystemCFI::LoadCAD()
Report();
}
// Tidy up
delete bds;
return true;
}
......@@ -330,6 +337,7 @@ void CADSystemCFI::AddCurve(int i, cfi::Line *in)
m_curves[i]);
m_verts[m_nameToVertId[vertList->at(1).entity->getName()]]->AddAdjCurve(
m_curves[i]);
delete vertList;
}
void CADSystemCFI::AddSurf(int i, cfi::Face *in)
......@@ -368,6 +376,7 @@ void CADSystemCFI::AddSurf(int i, cfi::Face *in)
fullEdgeList.push_back(*it2);
}
}
delete edgeList;
vector<EdgeLoopSharedPtr> edgeloops;
int done = 0;
......@@ -388,6 +397,8 @@ void CADSystemCFI::AddSurf(int i, cfi::Face *in)
edgeloop->edgeo.push_back(CADOrientation::eBackwards);
}
delete vertList;
edgeloop->edges.push_back(
m_curves[m_nameToCurveId[fullEdgeList.at(done).entity->getName()]]);
......@@ -412,6 +423,8 @@ void CADSystemCFI::AddSurf(int i, cfi::Face *in)
edgeloop->edgeo.push_back(CADOrientation::eBackwards);
}
delete vertList;
edgeloop->edges.push_back(
m_curves[m_nameToCurveId[
fullEdgeList.at(done).entity->getName()]]);
......
......@@ -37,7 +37,10 @@
#include "../CADSystem.h"
#ifndef NEK_CADFIXAPI_HXX
#define NEK_CADFIXAPI_HXX
#include "cadfixapi.hxx"
#endif
namespace Nektar
{
......@@ -60,7 +63,14 @@ public:
CADSystemCFI(std::string name) : CADSystem(name, "CFI")
{
}
~CADSystemCFI() = default;
~CADSystemCFI()
{
if (m_model != nullptr)
{
delete m_model;
}
}
bool LoadCAD();
......@@ -93,12 +103,13 @@ private:
void AddSurf(int i, cfi::Face *in);
cfi::Cfi m_cfiHandle;
cfi::Model *m_model;
cfi::Model *m_model = nullptr;
std::vector<cfi::Body* > m_bodies;
std::map<std::string, int> m_nameToVertId;
std::map<std::string, int> m_nameToCurveId;
std::map<std::string, int> m_nameToFaceId;
std::map<std::string, std::vector<std::string> > m_mapVertToListEdge;
NekDouble m_scal;
bool m_useCFIMesh = false;
};
......
......@@ -32,11 +32,8 @@
//
////////////////////////////////////////////////////////////////////////////////
#include "CADSystemCFI.h"
#include "CADVertCFI.h"
#include <NekMeshUtils/MeshElements/Node.h>
using namespace std;
namespace Nektar
......
......@@ -36,7 +36,11 @@
#define NEKMESHUTILS_CADSYSTEM_CFI_CADVERTCFI
#include "../CADVert.h"
#include <NekMeshUtils/MeshElements/Node.h>
#ifndef NEK_CADFIXAPI_HXX
#define NEK_CADFIXAPI_HXX
#include "cadfixapi.hxx"
#endif
namespace Nektar
{
......
......@@ -160,12 +160,12 @@ void Edge::MakeOrder(int order, SpatialDomains::GeometrySharedPtr geom,
m_edgeNodes[i]->m_y = loc[1];
m_edgeNodes[i]->m_z = loc[2];
vector<pair<CADSurfSharedPtr, CADOrientation::Orientation>> s =
vector<pair<weak_ptr<CADSurf>, CADOrientation::Orientation>> s =
c->GetAdjSurf();
for (int j = 0; j < s.size(); j++)
{
Array<OneD, NekDouble> uv = s[j].first->locuv(loc);
m_edgeNodes[i]->SetCADSurf(s[j].first, uv);
Array<OneD, NekDouble> uv = s[j].first.lock()->locuv(loc);
m_edgeNodes[i]->SetCADSurf(s[j].first.lock(), uv);
}
}
}
......
......@@ -125,7 +125,7 @@ public:
/// Distributions of points along edge.
LibUtilities::PointsType m_curveType;
/// Element(s) which are linked to this edge.
std::vector<std::pair<ElementSharedPtr, int> > m_elLink;
std::vector<std::pair<std::weak_ptr<Element>, int> > m_elLink;
CADObjectSharedPtr m_parentCAD;
......
......@@ -145,7 +145,7 @@ public:
/// Distribution of points in this face.
LibUtilities::PointsType m_curveType;
/// Element(s) which are linked to this face.
std::vector<std::pair<ElementSharedPtr, int> > m_elLink;
std::vector<std::pair<std::weak_ptr<Element>, int> > m_elLink;
/// Nektar++ representation of geometry
SpatialDomains::Geometry2DSharedPtr m_geom;
......
......@@ -274,7 +274,7 @@ public:
std::vector<CADCurveSharedPtr> lst;
for (auto &c : CADCurveList)
{
lst.push_back(c.second.first);
lst.push_back(c.second.first.lock());
}
return lst;
}
......@@ -284,7 +284,7 @@ public:
std::vector<CADSurfSharedPtr> lst;
for (auto &s : CADSurfList)
{
lst.push_back(s.second.first);
lst.push_back(s.second.first.lock());
}
return lst;
}
......@@ -304,7 +304,7 @@ public:
m_x = l[0];
m_y = l[1];
m_z = l[2];
CADSurfSharedPtr su = CADSurfList[s].first;
CADSurfSharedPtr su = CADSurfList[s].first.lock();
SetCADSurf(su, uv);
}
......@@ -314,7 +314,7 @@ public:
m_x = x;
m_y = y;
m_z = z;
CADSurfSharedPtr su = CADSurfList[s].first;
CADSurfSharedPtr su = CADSurfList[s].first.lock();
SetCADSurf(su, uv);
}
......@@ -323,7 +323,7 @@ public:
m_x = l[0];
m_y = l[1];
m_z = l[2];
CADCurveSharedPtr cu = CADCurveList[c].first;
CADCurveSharedPtr cu = CADCurveList[c].first.lock();
SetCADCurve(cu, t);
}
......@@ -332,7 +332,7 @@ public:
m_x = x;
m_y = y;
m_z = z;
CADCurveSharedPtr cu = CADCurveList[c].first;
CADCurveSharedPtr cu = CADCurveList[c].first.lock();
SetCADCurve(cu, t);
}
......@@ -412,9 +412,9 @@ public:
NekDouble m_z;
/// list of cadcurves the node lies on
std::map<int, std::pair<CADCurveSharedPtr, NekDouble>> CADCurveList;
std::map<int, std::pair<std::weak_ptr<CADCurve>, NekDouble>> CADCurveList;
/// list of cadsurfs the node lies on
std::map<int, std::pair<CADSurfSharedPtr, Array<OneD, NekDouble>>>
std::map<int, std::pair<std::weak_ptr<CADSurf>, Array<OneD, NekDouble>>>
CADSurfList;
private:
......
......@@ -250,8 +250,8 @@ void Module::ProcessEdges(bool ReprocessEdges)
elmt->SetEdgeLink(*it);
// Update 2D element boundary map.
pair<ElementSharedPtr, int> eMap = (*it)->m_elLink.at(0);
eMap.first->SetBoundaryLink(eMap.second, i);
pair<weak_ptr<Element>, int> eMap = (*it)->m_elLink.at(0);
eMap.first.lock()->SetBoundaryLink(eMap.second, i);
// Update vertices
elmt->SetVertex(0, (*it)->m_n1, false);
......@@ -367,8 +367,8 @@ void Module::ProcessFaces(bool ReprocessFaces)
// Update 3D element boundary map.
for (int j = 0; j < (*it)->m_elLink.size(); ++j)
{
pair<ElementSharedPtr, int> eMap = (*it)->m_elLink.at(j);
eMap.first->SetBoundaryLink(eMap.second, i);
pair<weak_ptr<Element>, int> eMap = (*it)->m_elLink.at(j);
eMap.first.lock()->SetBoundaryLink(eMap.second, i);
}
// Copy face curvature
......@@ -821,7 +821,7 @@ void Module::PrismLines(int prism,
if (it2 != perFaces.end())
{
int id2 = it2->second.first->m_id;
nextId = it2->second.first->m_elLink[0].first->GetId();
nextId = it2->second.first->m_elLink[0].first.lock()->GetId();
perFaces.erase(it2);
perFaces.erase(id2);
PrismLines(nextId, perFaces, prismsDone, line);
......@@ -833,10 +833,10 @@ void Module::PrismLines(int prism,
continue;
}
nextId = f->m_elLink[0].first->GetId();
nextId = f->m_elLink[0].first.lock()->GetId();
if (nextId == m_mesh->m_element[3][prism]->GetId())
{
nextId = f->m_elLink[1].first->GetId();
nextId = f->m_elLink[1].first.lock()->GetId();
}
PrismLines(nextId, perFaces, prismsDone, line);
......
......@@ -860,9 +860,9 @@ void Octree::CompileSourcePointList()
Array<OneD, NekDouble> loc = curve->P(t);
vector<pair<CADSurfSharedPtr, CADOrientation::Orientation> > ss =
vector<pair<weak_ptr<CADSurf>, CADOrientation::Orientation> > ss =
curve->GetAdjSurf();
Array<OneD, NekDouble> uv = ss[0].first->locuv(loc);
Array<OneD, NekDouble> uv = ss[0].first.lock()->locuv(loc);
if (C != 0.0)
{
......@@ -879,7 +879,7 @@ void Octree::CompileSourcePointList()
CPointSharedPtr newCPoint =
MemoryManager<CPoint>::AllocateSharedPtr(
ss[0].first->GetId(), uv, loc, del);
ss[0].first.lock()->GetId(), uv, loc, del);
m_SPList.push_back(newCPoint);
}
......@@ -887,7 +887,7 @@ void Octree::CompileSourcePointList()
{
BPointSharedPtr newBPoint =
MemoryManager<BPoint>::AllocateSharedPtr(
ss[0].first->GetId(), uv, loc);
ss[0].first.lock()->GetId(), uv, loc);
m_SPList.push_back(newBPoint);
}
......
......@@ -162,7 +162,7 @@ void CurveMesh::Mesh(bool forceThree)
Array<OneD, NekDouble> loc;
vector<CADVertSharedPtr> verts = m_cadcurve->GetVertex();
vector<pair<CADSurfSharedPtr, CADOrientation::Orientation> > s =
vector<pair<weak_ptr<CADSurf>, CADOrientation::Orientation> > s =
m_cadcurve->GetAdjSurf();
NodeSharedPtr n = verts[0]->GetNode();
......@@ -171,15 +171,15 @@ void CurveMesh::Mesh(bool forceThree)
loc = n->GetLoc();
for (int j = 0; j < s.size(); j++)
{
if (verts[0]->IsDegen() == s[j].first->GetId())
if (verts[0]->IsDegen() == s[j].first.lock()->GetId())
{
// if the degen has been set for this node the node
// already knows its corrected location
continue;
}
Array<OneD, NekDouble> uv = s[j].first->locuv(loc);
n->SetCADSurf(s[j].first, uv);
Array<OneD, NekDouble> uv = s[j].first.lock()->locuv(loc);
n->SetCADSurf(s[j].first.lock(), uv);
}
m_meshpoints.push_back(n);
......@@ -192,8 +192,8 @@ void CurveMesh::Mesh(bool forceThree)
n2->SetCADCurve(m_cadcurve, t);
for (int j = 0; j < s.size(); j++)
{
Array<OneD, NekDouble> uv = s[j].first->locuv(loc);
n2->SetCADSurf(s[j].first, uv);
Array<OneD, NekDouble> uv = s[j].first.lock()->locuv(loc);
n2->SetCADSurf(s[j].first.lock(), uv);
}
m_meshpoints.push_back(n2);
}
......@@ -204,15 +204,15 @@ void CurveMesh::Mesh(bool forceThree)
loc = n->GetLoc();
for (int j = 0; j < s.size(); j++)
{
if (verts[1]->IsDegen() == s[j].first->GetId())
if (verts[1]->IsDegen() == s[j].first.lock()->GetId())
{
// if the degen has been set for this node the node
// already knows its corrected location
continue;
}
Array<OneD, NekDouble> uv = s[j].first->locuv(loc);
n->SetCADSurf(s[j].first, uv);
Array<OneD, NekDouble> uv = s[j].first.lock()->locuv(loc);
n->SetCADSurf(s[j].first.lock(), uv);
}
m_meshpoints.push_back(n);
......@@ -417,7 +417,7 @@ void CurveMesh::PeriodicOverwrite(CurveMeshSharedPtr from)
vector<NodeSharedPtr> nodes = from->GetMeshPoints();
vector<pair<CADSurfSharedPtr, CADOrientation::Orientation> > surfs =
vector<pair<weak_ptr<CADSurf>, CADOrientation::Orientation> > surfs =
m_cadcurve->GetAdjSurf();
for (int i = 1; i < nodes.size() - 1; i++)
......@@ -428,8 +428,8 @@ void CurveMesh::PeriodicOverwrite(CurveMeshSharedPtr from)
for (int j = 0; j < surfs.size(); j++)
{
Array<OneD, NekDouble> uv = surfs[j].first->locuv(nn->GetLoc());
nn->SetCADSurf(surfs[j].first, uv);
Array<OneD, NekDouble> uv = surfs[j].first.lock()->locuv(nn->GetLoc());
nn->SetCADSurf(surfs[j].first.lock(), uv);
}
NekDouble t;
......
......@@ -579,8 +579,8 @@ void FaceMesh::DiagonalSwap()
continue;
}
ElementSharedPtr tri1 = e->m_elLink[0].first;
ElementSharedPtr tri2 = e->m_elLink[1].first;
ElementSharedPtr tri1 = e->m_elLink[0].first.lock();
ElementSharedPtr tri2 = e->m_elLink[1].first.lock();
NodeSharedPtr n1 = e->m_n1;
NodeSharedPtr n2 = e->m_n2;
......@@ -755,13 +755,13 @@ void FaceMesh::DiagonalSwap()
}
// now sort out links for the 4 edges surrounding the patch
vector<pair<ElementSharedPtr, int> > links;
vector<pair<weak_ptr<Element>, int> > links;
links = CA->m_elLink;
CA->m_elLink.clear();
for (int i = 0; i < links.size(); i++)
{
if (links[i].first->GetId() == tri1->GetId())
if (links[i].first.lock()->GetId() == tri1->GetId())
{
continue;
}
......@@ -772,7 +772,7 @@ void FaceMesh::DiagonalSwap()
BC->m_elLink.clear();
for (int i = 0; i < links.size(); i++)
{
if (links[i].first->GetId() == tri1->GetId())
if (links[i].first.lock()->GetId() == tri1->GetId())
{
continue;
}
......@@ -783,7 +783,7 @@ void FaceMesh::DiagonalSwap()
AD->m_elLink.clear();
for (int i = 0; i < links.size(); i++)
{
if (links[i].first->GetId() == tri2->GetId())
if (links[i].first.lock()->GetId() == tri2->GetId())
{
continue;
}
......@@ -794,7 +794,7 @@ void FaceMesh::DiagonalSwap()
DB->m_elLink.clear();
for (int i = 0; i < links.size(); i++)
{
if (links[i].first->GetId() == tri2->GetId())
if (links[i].first.lock()->GetId() == tri2->GetId())
{
continue;
}
......
......@@ -268,7 +268,7 @@ void HOSurfaceMesh::Process()
// need to pull the solution out of opti
ti = opti->GetSolution();
}
vector<pair<CADSurfSharedPtr, CADOrientation::Orientation>> s =
vector<pair<weak_ptr<CADSurf>, CADOrientation::Orientation>> s =
c->GetAdjSurf();
for (int k = 1; k < m_mesh->m_nummode - 1; k++)
......@@ -280,8 +280,9 @@ void HOSurfaceMesh::Process()
nn->SetCADCurve(c, ti[k]);
for (int m = 0; m < s.size(); m++)
{
Array<OneD, NekDouble> uv = s[m].first->locuv(loc);
nn->SetCADSurf(s[m].first, uv);
Array<OneD, NekDouble> uv =
s[m].first.lock()->locuv(loc);
nn->SetCADSurf(s[m].first.lock(), uv);
}
honodes[k - 1] = nn;
......
......@@ -119,11 +119,11 @@ void VolumeMesh::Process()
vector<EdgeSharedPtr> es = els[i]->GetEdgeList();
for (int j = 0; j < es.size(); j++)
{
vector<pair<ElementSharedPtr, int> > lk = es[j]->m_elLink;
vector<pair<weak_ptr<Element>, int> > lk = es[j]->m_elLink;
es[j]->m_elLink.clear();
for (int k = 0; k < lk.size(); k++)
{
if (lk[k].first == els[i])
if (lk[k].first.lock() == els[i])
{
continue;
}
......
......@@ -149,12 +149,12 @@ void InputCADfix::Process()
c->loct(xyz, t);
n->SetCADCurve(c, t);