From 783d125b7196816965e05ebae4d32b8fb566ff94 Mon Sep 17 00:00:00 2001 From: ponchio Date: Fri, 3 Dec 2004 21:19:00 +0000 Subject: [PATCH] Fixed a couple of memory leak... --- apps/nexus/decimate.cpp | 11 ++++--- apps/nexus/nxsbuilder.cpp | 37 ++++++++-------------- apps/nexus/nxsdispatcher.cpp | 6 ++-- apps/nexus/nxsserver.cpp | 4 +-- apps/nexus/remapping.cpp | 61 ++++++++++++++++++------------------ apps/nexus/vchain.cpp | 11 +++++-- apps/nexus/vchain.h | 3 +- 7 files changed, 65 insertions(+), 68 deletions(-) diff --git a/apps/nexus/decimate.cpp b/apps/nexus/decimate.cpp index 38921a1d..39ebe200 100644 --- a/apps/nexus/decimate.cpp +++ b/apps/nexus/decimate.cpp @@ -97,10 +97,10 @@ float nxs::Decimate(Decimation mode, float error; switch(mode) { - case CLUSTER: error = Cluster(mesh, target_faces); break; - case QUADRIC: error = Quadric(mesh, target_faces); break; - default: cerr << "Unknown simplification mode: " << mode << endl; - exit(0); + case CLUSTER: error = Cluster(mesh, target_faces); break; + case QUADRIC: error = Quadric(mesh, target_faces); break; + default: cerr << "Unknown simplification mode: " << mode << endl; + exit(0); } newvert.clear(); @@ -174,7 +174,8 @@ float Cluster(MyMesh &mesh, unsigned int target_faces) { unsigned int nseeds = target_faces/2; #ifndef NDEBUG if(nseeds >= mesh.vert.size()) { - cerr << "Strange! nseeds > vert.size(): " << nseeds << " >= "<< mesh.vert.size() << endl; + cerr << "Strange! nseeds > vert.size(): " << nseeds + << " >= "<< mesh.vert.size() << endl; } #endif diff --git a/apps/nexus/nxsbuilder.cpp b/apps/nexus/nxsbuilder.cpp index 542ef8c2..79a9c6b4 100644 --- a/apps/nexus/nxsbuilder.cpp +++ b/apps/nexus/nxsbuilder.cpp @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.7 2004/12/03 01:20:56 ponchio +Debug + Revision 1.6 2004/12/02 20:22:42 ponchio Level 5; @@ -396,10 +399,6 @@ void FourthStep(const string &crudefile, const string &output, } Report report(nexus.index.size()); -#ifndef NDEBUG - map > reciprocal; -#endif - for(int start = 0; start < nexus.index.size(); start++) { report.Step(start); Nexus::PatchInfo &s_entry = nexus.index[start]; @@ -418,29 +417,19 @@ void FourthStep(const string &crudefile, const string &output, Nexus::PatchInfo &e_entry = nexus.index[end]; float dist = Distance(s_entry.sphere, e_entry.sphere); - if(dist > (s_entry.sphere.Radius() + e_entry.sphere.Radius()) * 0.01) { -#ifndef NDEBUG - if(start > end) - assert(!reciprocal[end].count(start)); -#endif + if(dist > s_entry.sphere.Radius() + e_entry.sphere.Radius()) { continue; } - #ifndef NDEBUG - if(start > end) - assert(reciprocal[end].count(start)); - else - reciprocal[start].insert(end); -#endif for(unsigned int i = 0; i < vert_index[end].size; i++) { - unsigned int global = vert_remap[vert_index[end].offset + i]; - if(vremap.count(global)) { - Link link; - link.start_vert = vremap[global]; - link.end_vert = i; - link.end_patch = end; - links.push_back(link); - } + unsigned int global = vert_remap[vert_index[end].offset + i]; + if(vremap.count(global)) { + Link link; + link.start_vert = vremap[global]; + link.end_vert = i; + link.end_patch = end; + links.push_back(link); + } } } //TODO Horribili visu (interfaccia di cacca!) @@ -510,7 +499,7 @@ void FifthStep(const string &crudefile, const string &output, report.Step(fcount++); Fragment *fragin = new Fragment; - BuildFragment(nexus, vchain[level+1], + BuildFragment(nexus, *vchain[level+1], (*fragment).second, *fragin); dispatcher.SendFragment(fragin); diff --git a/apps/nexus/nxsdispatcher.cpp b/apps/nexus/nxsdispatcher.cpp index 42edcde7..0927b5df 100644 --- a/apps/nexus/nxsdispatcher.cpp +++ b/apps/nexus/nxsdispatcher.cpp @@ -15,18 +15,18 @@ void SaveFragment(Nexus &nexus, VChain &chain, void Opener::execute() { - cerr << "Trying to connect to: " << server->get_host() << endl; + // cerr << "Trying to connect to: " << server->get_host() << endl; server->reading.lock(); server->writing.lock(); while(1) { if(get_signaled()) return; - cerr << "Trying to connect to: " << server->get_host() << endl; + // cerr << "Trying to connect to: " << server->get_host() << endl; try { server->open(); server->connected = true; server->queue = 0; - cerr << "Connected to: " << server->get_host() << endl; + // cerr << "Connected to: " << server->get_host() << endl; break; } catch(...) { } diff --git a/apps/nexus/nxsserver.cpp b/apps/nexus/nxsserver.cpp index 3687cc13..bfff22fe 100644 --- a/apps/nexus/nxsserver.cpp +++ b/apps/nexus/nxsserver.cpp @@ -59,8 +59,8 @@ public: Join(fragin, newvert, newface, newbord); float error = Decimate(QUADRIC, - (unsigned int)((newface.size()/3) * 0.5), - newvert, newface, newbord); + (unsigned int)((newface.size()/3) * 0.5), + newvert, newface, newbord); message *outmsg = new message(MSG_USER); outmsg->param = (int)(new Fragment); diff --git a/apps/nexus/remapping.cpp b/apps/nexus/remapping.cpp index 908bba7f..e21d592a 100644 --- a/apps/nexus/remapping.cpp +++ b/apps/nexus/remapping.cpp @@ -45,24 +45,24 @@ void nxs::Remap(VChain &chain, float scaling, int steps) { - chain.push_back(VPartition()); - BuildPartition(chain.back(), points, target_size, min_size, max_size, steps); + VPartition *finepart = new VPartition; + // finepart.Init(); + chain.push_back(finepart); + BuildPartition(*finepart, points, target_size, min_size, max_size, steps); - - chain.push_back(VPartition()); - BuildPartition(chain.back(), points, + VPartition *coarsepart = new VPartition; + // coarsepart.Init(); + chain.push_back(coarsepart); + BuildPartition(*coarsepart, points, (int)(target_size/scaling), min_size, max_size, steps); - VPartition &finepart = chain[0]; - finepart.Init(); - cerr << "Fine size: " << finepart.size() << endl; - VPartition &coarsepart = chain[1]; - coarsepart.Init(); - cerr << "Coarse size: " << coarsepart.size() << endl; + + cerr << "Fine size: " << finepart->size() << endl; + cerr << "Coarse size: " << coarsepart->size() << endl; typedef map, unsigned int> FragIndex; @@ -75,8 +75,8 @@ void nxs::Remap(VChain &chain, for(unsigned int i = 0; i < points.Size(); i++) { bari = points[i]; - unsigned int fine = finepart.Locate(bari); - unsigned int coarse = coarsepart.Locate(bari); + unsigned int fine = finepart->Locate(bari); + unsigned int coarse = coarsepart->Locate(bari); unsigned int patch; @@ -239,11 +239,12 @@ void nxs::BuildLevel(VChain &chain, totface += nexus.index[idx].nface; totvert += nexus.index[idx].nvert; } - - chain.push_back(VPartition()); - VPartition &coarse = chain[chain.size()-1]; - VPartition &fine = chain[chain.size()-2]; - fine.Init(); + + VPartition *fine = chain[chain.size()-1]; + fine->Init(); + + VPartition *coarse = new VPartition; + chain.push_back(coarse); //unsigned int ncells = (unsigned int)(fine.size() * scaling); unsigned int ncells = (unsigned int)(scaling * totface/target_size); @@ -256,21 +257,21 @@ void nxs::BuildLevel(VChain &chain, if(cratio > 1) { Patch patch = nexus.GetPatch(idx); Point3f &v = patch.Vert(0); - coarse.push_back(v); + coarse->push_back(v); cratio -= 1; } } - if(coarse.size() == 0) { + if(coarse->size() == 0) { Patch patch = nexus.GetPatch(0); - coarse.push_back(patch.Vert(0)); + coarse->push_back(patch.Vert(0)); } - float coarse_vmean = totface/(float)coarse.size(); + float coarse_vmean = totface/(float)coarse->size(); - coarse.Init(); + coarse->Init(); cerr << "Ncells: " << ncells << endl; - cerr << "Coarse size: " << coarse.size() << endl; + cerr << "Coarse size: " << coarse->size() << endl; cerr << "Coarse mean: " << coarse_vmean << " mean_size: " << target_size << endl; //here goes some optimization pass. @@ -282,8 +283,8 @@ void nxs::BuildLevel(VChain &chain, cerr << "Optimization step: " << step+1 << "/" << steps << endl; centroids.clear(); counts.clear(); - centroids.resize(coarse.size(), Point3f(0, 0, 0)); - counts.resize(coarse.size(), 0); + centroids.resize(coarse->size(), Point3f(0, 0, 0)); + counts.resize(coarse->size(), 0); Report report(nexus.index.size()); for(unsigned int idx = offset; idx < nexus.index.size(); idx++) { @@ -295,8 +296,8 @@ void nxs::BuildLevel(VChain &chain, patch.Vert(face[1]) + patch.Vert(face[2]))/3; - unsigned int target = coarse.Locate(bari); - assert(target < coarse.size()); + unsigned int target = coarse->Locate(bari); + assert(target < coarse->size()); centroids[target] += bari; counts[target]++; } @@ -307,11 +308,11 @@ void nxs::BuildLevel(VChain &chain, centroids[v]/= counts[v]; if(step == steps-1) { - if(!Optimize(coarse, (int)coarse_vmean, min_size, max_size, + if(!Optimize(*coarse, (int)coarse_vmean, min_size, max_size, centroids, counts, false)) step--; } else - Optimize(coarse, (int)coarse_vmean, min_size, max_size, + Optimize(*coarse, (int)coarse_vmean, min_size, max_size, centroids, counts, true); } chain.newfragments.clear(); diff --git a/apps/nexus/vchain.cpp b/apps/nexus/vchain.cpp index 776e179e..02a96b90 100644 --- a/apps/nexus/vchain.cpp +++ b/apps/nexus/vchain.cpp @@ -6,6 +6,11 @@ using namespace std; using namespace vcg; using namespace nxs; +VChain::~VChain() { + for(iterator i = begin(); i != end(); i++) + delete *i; +} + bool VChain::Save(const string &file) { FILE *fp = fopen(file.c_str(), "wb+"); if(!fp) { @@ -16,7 +21,7 @@ bool VChain::Save(const string &file) { unsigned int nlevels = size(); fwrite(&nlevels, sizeof(unsigned int), 1, fp); for(unsigned int i = 0; i < nlevels; i++) { - VPartition &level = operator[](i); + VPartition &level = *operator[](i); unsigned int npoints = level.size(); fwrite(&npoints, sizeof(unsigned int), 1, fp); fwrite(&(level[0]), sizeof(Point3f), npoints, fp); @@ -59,8 +64,8 @@ bool VChain::Load(const string &file) { unsigned int nlevels; fread(&nlevels, sizeof(unsigned int), 1, fp); for(unsigned int i = 0; i < nlevels; i++) { - push_back(VPartition()); - VPartition &level = back(); + push_back(new VPartition()); + VPartition &level = *back(); unsigned int npoints; fread(&npoints, sizeof(unsigned int), 1, fp); diff --git a/apps/nexus/vchain.h b/apps/nexus/vchain.h index 163fc480..8fe3a9cc 100644 --- a/apps/nexus/vchain.h +++ b/apps/nexus/vchain.h @@ -8,8 +8,9 @@ namespace nxs { -class VChain: public std::vector { +class VChain: public std::vector { public: + ~VChain(); bool Save(const std::string &file); bool Load(const std::string &file);