From 22d054bc89868697b430d6f248624c6aa075772a Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Tue, 26 Feb 2013 13:00:06 +0000 Subject: [PATCH] fix mem-leaks, autoptr geometry --- libsrc/csg/csgpkg.cpp | 31 ++++++++------- libsrc/geom2d/geometry2d.cpp | 2 +- libsrc/interface/nginterface.cpp | 68 ++++++++------------------------ libsrc/meshing/Makefile.am | 6 ++- libsrc/meshing/curvedelems.cpp | 8 ++-- libsrc/meshing/meshclass.cpp | 35 +++++----------- 6 files changed, 51 insertions(+), 99 deletions(-) diff --git a/libsrc/csg/csgpkg.cpp b/libsrc/csg/csgpkg.cpp index e118de00..589d12ff 100644 --- a/libsrc/csg/csgpkg.cpp +++ b/libsrc/csg/csgpkg.cpp @@ -17,7 +17,8 @@ extern "C" int Ng_CSG_Init (Tcl_Interp * interp); namespace netgen { - extern DLL_HEADER NetgenGeometry * ng_geometry; + // extern DLL_HEADER NetgenGeometry * ng_geometry; + extern DLL_HEADER AutoPtr ng_geometry; extern DLL_HEADER AutoPtr mesh; static VisualSceneGeometry vsgeom; @@ -33,7 +34,7 @@ namespace netgen Tcl_Interp * interp, int argc, tcl_const char *argv[]) { - CSGeometry * csgeom = dynamic_cast (ng_geometry); + CSGeometry * csgeom = dynamic_cast (ng_geometry.Ptr()); if (csgeom) { double detail = atof (Tcl_GetVar (interp, "::geooptions.detail", 0)); @@ -52,7 +53,7 @@ namespace netgen Tcl_Interp * interp, int argc, tcl_const char *argv[]) { - CSGeometry * geometry = dynamic_cast (ng_geometry); + CSGeometry * geometry = dynamic_cast (ng_geometry.Ptr()); const char * command = argv[1]; @@ -107,7 +108,7 @@ namespace netgen Tcl_Interp * interp, int argc, tcl_const char *argv[]) { - CSGeometry * geometry = dynamic_cast (ng_geometry); + CSGeometry * geometry = dynamic_cast (ng_geometry.Ptr()); if (!geometry) { Tcl_SetResult (interp, err_needscsgeometry, TCL_STATIC); @@ -142,7 +143,7 @@ namespace netgen Tcl_Interp * interp, int argc, tcl_const char *argv[]) { - CSGeometry * geometry = dynamic_cast (ng_geometry); + CSGeometry * geometry = dynamic_cast (ng_geometry.Ptr()); if (!geometry) { Tcl_SetResult (interp, err_needscsgeometry, TCL_STATIC); @@ -180,7 +181,7 @@ namespace netgen Tcl_Interp * interp, int argc, tcl_const char *argv[]) { - CSGeometry * geometry = dynamic_cast (ng_geometry); + CSGeometry * geometry = dynamic_cast (ng_geometry.Ptr()); if (!geometry) { Tcl_SetResult (interp, err_needscsgeometry, TCL_STATIC); @@ -207,7 +208,7 @@ namespace netgen Tcl_Interp * interp, int argc, tcl_const char *argv[]) { - CSGeometry * geometry = dynamic_cast (ng_geometry); + CSGeometry * geometry = dynamic_cast (ng_geometry.Ptr()); if (!geometry) { Tcl_SetResult (interp, err_needscsgeometry, TCL_STATIC); @@ -245,7 +246,7 @@ namespace netgen Tcl_Interp * interp, int argc, tcl_const char *argv[]) { - CSGeometry * geometry = dynamic_cast (ng_geometry); + CSGeometry * geometry = dynamic_cast (ng_geometry.Ptr()); if (!geometry) { Tcl_SetResult (interp, err_needscsgeometry, TCL_STATIC); @@ -272,7 +273,7 @@ namespace netgen Tcl_Interp * interp, int argc, tcl_const char *argv[]) { - CSGeometry * geometry = dynamic_cast (ng_geometry); + CSGeometry * geometry = dynamic_cast (ng_geometry.Ptr()); if (!geometry) { Tcl_SetResult (interp, err_needscsgeometry, TCL_STATIC); @@ -305,7 +306,7 @@ namespace netgen Tcl_Interp * interp, int argc, tcl_const char *argv[]) { - CSGeometry * geometry = dynamic_cast (ng_geometry); + CSGeometry * geometry = dynamic_cast (ng_geometry.Ptr()); if (!geometry) { Tcl_SetResult (interp, err_needscsgeometry, TCL_STATIC); @@ -336,7 +337,7 @@ namespace netgen Tcl_Interp * interp, int argc, tcl_const char *argv[]) { - CSGeometry * geometry = dynamic_cast (ng_geometry); + CSGeometry * geometry = dynamic_cast (ng_geometry.Ptr()); if (!geometry) { Tcl_SetResult (interp, err_needscsgeometry, TCL_STATIC); @@ -367,7 +368,7 @@ namespace netgen Tcl_Interp * interp, int argc, tcl_const char *argv[]) { - CSGeometry * geometry = dynamic_cast (ng_geometry); + CSGeometry * geometry = dynamic_cast (ng_geometry.Ptr()); if (!geometry) { Tcl_SetResult (interp, err_needscsgeometry, TCL_STATIC); @@ -492,7 +493,7 @@ namespace netgen Tcl_Interp * interp, int argc, tcl_const char *argv[]) { - CSGeometry * geometry = dynamic_cast (ng_geometry); + CSGeometry * geometry = dynamic_cast (ng_geometry.Ptr()); if (!geometry) { Tcl_SetResult (interp, err_needscsgeometry, TCL_STATIC); @@ -521,7 +522,7 @@ namespace netgen Tcl_Interp * interp, int argc, tcl_const char *argv[]) { - CSGeometry * geometry = dynamic_cast (ng_geometry); + CSGeometry * geometry = dynamic_cast (ng_geometry.Ptr()); if (!geometry) { Tcl_SetResult (interp, err_needscsgeometry, TCL_STATIC); @@ -610,7 +611,7 @@ namespace netgen VisualScene * CSGeometryRegister :: GetVisualScene (const NetgenGeometry * geom) const { - CSGeometry * geometry = dynamic_cast (ng_geometry); + CSGeometry * geometry = dynamic_cast (ng_geometry.Ptr()); if (geometry) { vsgeom.SetGeometry (geometry); diff --git a/libsrc/geom2d/geometry2d.cpp b/libsrc/geom2d/geometry2d.cpp index 83189727..23b29596 100644 --- a/libsrc/geom2d/geometry2d.cpp +++ b/libsrc/geom2d/geometry2d.cpp @@ -17,7 +17,7 @@ namespace netgen for ( int i = 0; i < bcnames.Size(); i++ ) delete bcnames[i]; for (int i=0; i mesh; - NetgenGeometry * ng_geometry = NULL; // new NetgenGeometry; + // NetgenGeometry * ng_geometry = NULL; // new NetgenGeometry; + AutoPtr ng_geometry; // extern NetgenGeometry * ng_geometry; // extern AutoPtr mesh; @@ -145,9 +146,12 @@ void Ng_LoadGeometry (const char * filename) NetgenGeometry * hgeom = geometryregister[i]->Load (filename); if (hgeom) { + /* delete ng_geometry; ng_geometry = hgeom; - + */ + ng_geometry.Reset (hgeom); + mesh.Reset(); return; } @@ -157,8 +161,11 @@ void Ng_LoadGeometry (const char * filename) // can be used to reset geometry if (strcmp(filename,"")==0) { + /* delete ng_geometry; ng_geometry = new NetgenGeometry(); + */ + ng_geometry.Reset (new NetgenGeometry()); return; } @@ -177,55 +184,14 @@ void Ng_LoadMeshFromStream ( istream & input ) NetgenGeometry * hgeom = geometryregister[i]->LoadFromMeshFile (input); if (hgeom) { + /* delete ng_geometry; ng_geometry = hgeom; + */ + ng_geometry.Reset (hgeom); break; } } - - -#ifdef LOADOLD - if(input.good()) - { - string auxstring; - input >> auxstring; - if(auxstring == "csgsurfaces") - { - /* - if (geometry) - { - geometry.Reset (new CSGeometry ("")); - } - if (stlgeometry) - { - delete stlgeometry; - stlgeometry = NULL; - } - #ifdef OCCGEOMETRY - if (occgeometry) - { - delete occgeometry; - occgeometry = NULL; - } - #endif - #ifdef ACIS - if (acisgeometry) - { - delete acisgeometry; - acisgeometry = NULL; - } - #endif - geometry2d.Reset (0); - */ - // geometry -> LoadSurfaces(input); - CSGeometry * geometry = new CSGeometry (""); - geometry -> LoadSurfaces(input); - - delete ng_geometry; - ng_geometry = geometry; - } - } -#endif } @@ -236,9 +202,11 @@ void Ng_LoadMesh (const char * filename) #ifdef PARALLEL MPI_Comm_size(MPI_COMM_WORLD, &ntasks); MPI_Comm_rank(MPI_COMM_WORLD, &id); -#endif + if (id == 0) { +#endif + if ( (strlen (filename) > 4) && strcmp (filename + (strlen (filename)-4), ".vol") != 0 ) { @@ -318,9 +286,7 @@ void Ng_LoadMesh (const char * filename) mesh -> Distribute(volume_weights, surface_weights, segment_weights); } } -#endif } -#ifdef PARALLEL else { mesh.Reset (new Mesh()); @@ -688,7 +654,7 @@ void Ng_GetNormalVector (int sei, int locpi, double * nv) nv[2] = n(2); } #endif - CSGeometry * geometry = dynamic_cast (ng_geometry); + CSGeometry * geometry = dynamic_cast (ng_geometry.Ptr()); if (geometry) { n = geometry->GetSurface (surfi) -> GetNormalVector(p); @@ -2175,7 +2141,7 @@ int Ng_Bisect_WithInfo ( const char * refinementfile, double ** qualityloss, int #endif { // ref = new RefinementSurfaces(*geometry); - CSGeometry * geometry = dynamic_cast (ng_geometry); + CSGeometry * geometry = dynamic_cast (ng_geometry.Ptr()); if (geometry) { opt = new MeshOptimize2dSurfaces(*geometry); diff --git a/libsrc/meshing/Makefile.am b/libsrc/meshing/Makefile.am index c4ca1ec0..3ea043cc 100644 --- a/libsrc/meshing/Makefile.am +++ b/libsrc/meshing/Makefile.am @@ -31,5 +31,7 @@ libmesh_la_SOURCES = adfront2.cpp adfront3.cpp bisect.cpp boundarylayer.cpp \ parallelmesh.cpp paralleltop.cpp paralleltop.hpp basegeom.cpp libmesh_la_LIBADD = $(top_builddir)/libsrc/linalg/libla.la \ - $(top_builddir)/libsrc/gprim/libgprim.la \ - $(top_builddir)/libsrc/general/libgen.la + $(top_builddir)/libsrc/gprim/libgprim.la \ + $(top_builddir)/libsrc/general/libgen.la \ + -lz + diff --git a/libsrc/meshing/curvedelems.cpp b/libsrc/meshing/curvedelems.cpp index d0a30973..8e5887df 100644 --- a/libsrc/meshing/curvedelems.cpp +++ b/libsrc/meshing/curvedelems.cpp @@ -211,17 +211,15 @@ namespace netgen RecPol (int amaxorder) { maxorder = amaxorder; - // cout << "maxo = " << maxorder << endl; - a = new double[maxorder+1]; b = new double[maxorder+1]; c = new double[maxorder+1]; } ~RecPol () { - delete a; - delete b; - delete c; + delete [] a; + delete [] b; + delete [] c; } template diff --git a/libsrc/meshing/meshclass.cpp b/libsrc/meshing/meshclass.cpp index 5bf83367..e6acd79c 100644 --- a/libsrc/meshing/meshclass.cpp +++ b/libsrc/meshing/meshclass.cpp @@ -361,6 +361,7 @@ namespace netgen { ofstream outfile(filename.c_str()); + // ogzstream outfile( (filename+".gz") .c_str()); Save(outfile); } @@ -408,54 +409,38 @@ namespace netgen { if ((*this)[sei].GetIndex()) { - outfile.width(8); - outfile << GetFaceDescriptor((*this)[sei].GetIndex ()).SurfNr()+1; - outfile.width(8); - outfile << GetFaceDescriptor((*this)[sei].GetIndex ()).BCProperty(); - outfile.width(8); - outfile << GetFaceDescriptor((*this)[sei].GetIndex ()).DomainIn(); - outfile.width(8); - outfile << GetFaceDescriptor((*this)[sei].GetIndex ()).DomainOut(); + outfile << " " << GetFaceDescriptor((*this)[sei].GetIndex ()).SurfNr()+1; + outfile << " " << GetFaceDescriptor((*this)[sei].GetIndex ()).BCProperty(); + outfile << " " << GetFaceDescriptor((*this)[sei].GetIndex ()).DomainIn(); + outfile << " " << GetFaceDescriptor((*this)[sei].GetIndex ()).DomainOut(); } else - outfile << " 0 0 0"; + outfile << " 0 0 0"; Element2d sel = (*this)[sei]; if (invertsurf) sel.Invert(); - outfile.width(8); - outfile << sel.GetNP(); - + outfile << " " << sel.GetNP(); for (j = 0; j < sel.GetNP(); j++) - { - outfile.width(8); - outfile << sel[j]; - } - + outfile << " " << sel[j]; switch (geomtype) { case GEOM_STL: for (j = 1; j <= sel.GetNP(); j++) - { - outfile.width(7); - outfile << " " << sel.GeomInfoPi(j).trignum; - } + outfile << " " << sel.GeomInfoPi(j).trignum; break; case GEOM_OCC: case GEOM_ACIS: for (j = 1; j <= sel.GetNP(); j++) { - outfile.width(7); outfile << " " << sel.GeomInfoPi(j).u; outfile << " " << sel.GeomInfoPi(j).v; } break; default: - ; // outfile << "\n"; + ; } - - outfile << "\n"; }