From bed187faa5c774b4b5efd23d48c2c618e84bcc7a Mon Sep 17 00:00:00 2001 From: Durk Talsma Date: Wed, 24 Aug 2011 19:47:18 +0200 Subject: [PATCH] Fix memory leaks in terrafit. --- src/Lib/Array/array.cxx | 13 +++++++++-- src/Lib/Array/array.hxx | 2 +- src/Prep/Terra/GreedyInsert.cc | 8 ++++++- src/Prep/Terra/GreedyInsert.h | 1 + src/Prep/Terra/Quadedge.cc | 6 ++--- src/Prep/Terra/Subdivision.cc | 40 ++++++++++++++++++++++++++++++---- src/Prep/Terra/Subdivision.h | 14 +++++++++++- src/Prep/TerraFit/terrafit.cc | 2 ++ 8 files changed, 74 insertions(+), 12 deletions(-) diff --git a/src/Lib/Array/array.cxx b/src/Lib/Array/array.cxx index 419b28da..608ab0d0 100644 --- a/src/Lib/Array/array.cxx +++ b/src/Lib/Array/array.cxx @@ -48,7 +48,10 @@ TGArray::TGArray( void ): fitted_in(NULL) { // cout << "class TGArray CONstructor called." << endl; - in_data = new int[ARRAY_SIZE_1][ARRAY_SIZE_1]; + //in_data = new int[ARRAY_SIZE_1][ARRAY_SIZE_1]; + in_data = new int*[ARRAY_SIZE_1]; + for (int i = 0; i < ARRAY_SIZE_1; i++) + in_data[i] = new int[ARRAY_SIZE_1]; // out_data = new float[ARRAY_SIZE_1][ARRAY_SIZE_1]; } @@ -58,7 +61,9 @@ TGArray::TGArray( const string &file ): fitted_in(NULL) { // cout << "class TGArray CONstructor called." << endl; - in_data = new int[ARRAY_SIZE_1][ARRAY_SIZE_1]; + in_data = new int* [ARRAY_SIZE_1]; + for (int i = 0; i < ARRAY_SIZE_1; i++) + in_data[i] = new int[ARRAY_SIZE_1]; // out_data = new float[ARRAY_SIZE_1][ARRAY_SIZE_1]; TGArray::open(file); @@ -101,6 +106,8 @@ bool TGArray::close() { // the sg_gzifstream doesn't seem to have a close() + array_in->close(); + fitted_in->close(); delete array_in; delete fitted_in; @@ -754,6 +761,8 @@ void TGArray::outputmesh_output_nodes( const string& fg_root, SGBucket& p ) TGArray::~TGArray( void ) { // printf("class TGArray DEstructor called.\n"); + for (int i = 0; i < ARRAY_SIZE_1; i++) + delete [] in_data[i]; delete [] in_data; // delete [] out_data; } diff --git a/src/Lib/Array/array.hxx b/src/Lib/Array/array.hxx index ec5462d1..562e7169 100644 --- a/src/Lib/Array/array.hxx +++ b/src/Lib/Array/array.hxx @@ -63,7 +63,7 @@ private: double col_step, row_step; // pointers to the actual grid data allocated here - int (*in_data)[ARRAY_SIZE_1]; + int **in_data; // float (*out_data)[ARRAY_SIZE_1]; // output nodes diff --git a/src/Prep/Terra/GreedyInsert.cc b/src/Prep/Terra/GreedyInsert.cc index 2659bb1e..22be0b6f 100644 --- a/src/Prep/Terra/GreedyInsert.cc +++ b/src/Prep/Terra/GreedyInsert.cc @@ -65,6 +65,12 @@ GreedySubdivision::GreedySubdivision(Map *map) count = 4; } +GreedySubdivision::~GreedySubdivision() +{ + delete heap; + is_used.free(); +} + @@ -72,6 +78,7 @@ GreedySubdivision::GreedySubdivision(Map *map) Triangle *GreedySubdivision::allocFace(Edge *e) { Triangle *t = new TrackedTriangle(e); + triangles.push_back(t); heap->insert(t, -1.0); @@ -248,7 +255,6 @@ int GreedySubdivision::greedyInsert() T.getCandidate(&sx, &sy); select(sx, sy, &T); - return True; } diff --git a/src/Prep/Terra/GreedyInsert.h b/src/Prep/Terra/GreedyInsert.h index 3b4b8167..72f04991 100644 --- a/src/Prep/Terra/GreedyInsert.h +++ b/src/Prep/Terra/GreedyInsert.h @@ -68,6 +68,7 @@ protected: public: GreedySubdivision(Map *map); + ~GreedySubdivision(); array2 is_used; diff --git a/src/Prep/Terra/Quadedge.cc b/src/Prep/Terra/Quadedge.cc index 9ae5b1bd..91520269 100644 --- a/src/Prep/Terra/Quadedge.cc +++ b/src/Prep/Terra/Quadedge.cc @@ -63,9 +63,9 @@ Edge::~Edge() e2->qnext = NULL; e3->qnext = NULL; - delete e1; - delete e2; - delete e3; + //delete e1; + //delete e2; + //delete e3; } } diff --git a/src/Prep/Terra/Subdivision.cc b/src/Prep/Terra/Subdivision.cc index b20611f3..12cda725 100644 --- a/src/Prep/Terra/Subdivision.cc +++ b/src/Prep/Terra/Subdivision.cc @@ -10,16 +10,45 @@ using std::endl; namespace Terra { +Subdivision::Subdivision() +{ + startingEdge = 0; + first_face = 0; +} + +Subdivision::~Subdivision() +{ + //delete [] startingEdge; + //delete [] first_face; + for (TriangleVecIterator tri= triangles.begin(); tri != triangles.end(); tri++) { + delete (*tri); + } + for (EdgeVecIterator e = edges.begin(); e != edges.end(); e++) { + delete (*e); + } +} + Edge *Subdivision::makeEdge(Vec2& org, Vec2& dest) { Edge *e = new Edge(); + edges.push_back(e); + edges.push_back(e->Rot()); + edges.push_back(e->Rot()->Rot()); + edges.push_back(e->Rot()->Rot()->Rot()); e->EndPoints(org, dest); + return e; } Edge *Subdivision::makeEdge() { - return new Edge(); + Edge *e = new Edge(); + edges.push_back(e); + edges.push_back(e->Rot()); + edges.push_back(e->Rot()->Rot()); + edges.push_back(e->Rot()->Rot()->Rot()); + + return e; } void Subdivision::initMesh(const Vec2& A,const Vec2& B, @@ -67,7 +96,8 @@ void Subdivision::deleteEdge(Edge *e) splice(e, e->Oprev()); splice(e->Sym(), e->Sym()->Oprev()); - delete e; + // Note that the Subdivision destructor takes care of the physical deletion now. + //delete e; } Edge *Subdivision::connect(Edge *a, Edge *b) @@ -370,7 +400,7 @@ void Subdivision::optimize(Vec2& x, Edge *s) } while( spoke != start_spoke ); } -Edge *Subdivision::insert(Vec2& x, Triangle *tri) +Edge *Subdivision::insert(Vec2& x, Triangle *tri) { Edge *e = tri?locate(x, tri->getAnchor()):locate(x); @@ -386,7 +416,9 @@ Edge *Subdivision::insert(Vec2& x, Triangle *tri) Triangle *Subdivision::allocFace(Edge *e) { - return new Triangle(e); + Triangle *t = new Triangle(e); + triangles.push_back(t); + return t; } Triangle& Subdivision::makeFace(Edge *e) diff --git a/src/Prep/Terra/Subdivision.h b/src/Prep/Terra/Subdivision.h index effd4b61..338d83f0 100644 --- a/src/Prep/Terra/Subdivision.h +++ b/src/Prep/Terra/Subdivision.h @@ -4,6 +4,7 @@ #include "Quadedge.h" #include +#include namespace Terra { @@ -38,15 +39,26 @@ public: typedef void (*edge_callback)(Edge *, void *); typedef void (*face_callback)(Triangle&, void *); +typedef std::vector TriangleVec; +typedef std::vector::iterator TriangleVecIterator; + +typedef std::vector EdgeVec; +typedef std::vector::iterator EdgeVecIterator; + class Subdivision { private: Edge *startingEdge; Triangle *first_face; + EdgeVec edges; + protected: + + TriangleVec triangles; void initMesh(const Vec2&, const Vec2&, const Vec2&, const Vec2&); - Subdivision() { } + Subdivision(); + ~Subdivision(); Edge *makeEdge(); Edge *makeEdge(Vec2& org, Vec2& dest); diff --git a/src/Prep/TerraFit/terrafit.cc b/src/Prep/TerraFit/terrafit.cc index 6a09bc76..9de8cdfd 100644 --- a/src/Prep/TerraFit/terrafit.cc +++ b/src/Prep/TerraFit/terrafit.cc @@ -203,6 +203,8 @@ void fit_file(const std::string& path) { gzprintf(fp,"%+03.8f %+02.8f %0.2f\n",vx,vy,vz); } } + delete mesh; + delete DEM; gzclose(fp); }