Skip to content

Commit e28ca40

Browse files
authored
Merge pull request #364 from LIHPC-Computational-Geometry/360-memory-leaks-in-the-cadfac-component-use-of-ann
fix memory leak related to ANN in cadfac
2 parents 61da281 + fbfa956 commit e28ca40

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

cadfac/inc/gmds/cadfac/FACSurface.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
#include <gmds/math/Triangle.h>
1717
#include "GMDSCadFac_export.h"
1818
/*----------------------------------------------------------------------------*/
19+
#include <ANN/ANN.h>
1920
#include <tuple>
2021
/*----------------------------------------------------------------------------*/
21-
class ANNkd_tree;
22-
/*----------------------------------------------------------------------------*/
2322
namespace gmds{
2423
/*----------------------------------------------------------------------------*/
2524
namespace cad{
@@ -198,6 +197,7 @@ namespace gmds{
198197
std::vector<GeomVolume*> m_adjacent_volumes;
199198

200199
/** kd tree structure used to make geometric queries faster*/
200+
ANNpointArray m_dataPts;
201201
ANNkd_tree* m_kd_tree;
202202
};
203203
/*----------------------------------------------------------------------------*/

cadfac/src/FACSurface.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ FACSurface::~FACSurface()
4848
{
4949
// got to clean some technical attributes used by ANN
5050
delete m_kd_tree;
51+
annDeallocPts(m_dataPts);
5152
annClose(); // done with ANN
5253
}
5354
/*----------------------------------------------------------------------------*/
@@ -166,7 +167,10 @@ FACSurface::setMeshFaces(const std::vector<Face> &AFaces)
166167
m_mesh_faces[i] = AFaces[i].id();
167168
}
168169

169-
if (m_kd_tree != NULL) delete m_kd_tree;
170+
if (m_kd_tree != nullptr) {
171+
delete m_kd_tree;
172+
annDeallocPts(m_dataPts);
173+
}
170174

171175
buildANNTree();
172176
}
@@ -338,11 +342,11 @@ FACSurface::volumes()
338342
void
339343
FACSurface::buildANNTree()
340344
{
345+
if (m_kd_tree != nullptr) throw GMDSException("FACSurface Issue: the kd tree structure was previously initialized");
341346

342347
int dim = 3; // dimension
343348
int maxPts = m_mesh_faces.size(); // maximum number of data points
344-
ANNpointArray dataPts; // data points
345-
dataPts = annAllocPts(maxPts, dim); // allocate data points
349+
m_dataPts = annAllocPts(maxPts, dim); // allocate data points
346350

347351
//========================================================
348352
// (1) Fill in the ANN structure for storing points
@@ -353,16 +357,15 @@ FACSurface::buildANNTree()
353357

354358
while (nPts < maxPts) {
355359
math::Point p = m_support->get<Face>(m_mesh_faces[nPts]).center();
356-
dataPts[nPts][0] = p.X();
357-
dataPts[nPts][1] = p.Y();
358-
dataPts[nPts][2] = p.Z();
360+
m_dataPts[nPts][0] = p.X();
361+
m_dataPts[nPts][1] = p.Y();
362+
m_dataPts[nPts][2] = p.Z();
359363
nPts++;
360364
};
361365
//========================================================
362366
// (2) Build the search structure
363367
//========================================================
364-
if (m_kd_tree != NULL) throw GMDSException("FACSurface Issue: the kd tree structure was previously initialized");
365-
m_kd_tree = new ANNkd_tree(dataPts, // the data points
368+
m_kd_tree = new ANNkd_tree(m_dataPts, // the data points
366369
nPts, // number of points
367370
dim); // dimension of space
368371
}
@@ -388,6 +391,8 @@ FACSurface::getANNClosestTriangle(const math::Point &AP) const
388391
queryPt, // query point
389392
k, nnIdx, dists, 0.01);
390393
int idx = nnIdx[0];
394+
395+
annDeallocPt(queryPt);
391396
delete[] nnIdx;
392397
delete[] dists;
393398

0 commit comments

Comments
 (0)