[MOAB-dev] commit/MOAB: danwu: Fix memory leak for three files in src folder and one file in examples folder.

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Tue Sep 24 14:17:47 CDT 2013


1 new commit in MOAB:

https://bitbucket.org/fathomteam/moab/commits/a5ccd6ba1a61/
Changeset:   a5ccd6ba1a61
Branch:      master
User:        danwu
Date:        2013-09-24 21:17:36
Summary:     Fix memory leak for three files in src folder and one file in examples folder.

Affected #:  4 files

diff --git a/examples/old/simple/GetEntities.cpp b/examples/old/simple/GetEntities.cpp
index 8368d9f..31d24d7 100644
--- a/examples/old/simple/GetEntities.cpp
+++ b/examples/old/simple/GetEntities.cpp
@@ -11,8 +11,10 @@ int main(int argc, char **argv) {
   // Instantiate & load a mesh from a file
   moab::Core *mb = new moab::Core();
   moab::ErrorCode rval = mb->load_mesh(argv[1]);
-  if (moab::MB_SUCCESS != rval)
+  if (moab::MB_SUCCESS != rval) {
+    delete mb;
     return 1;
+  }
 
   moab::Range ents;
 
@@ -20,8 +22,10 @@ int main(int argc, char **argv) {
   for (int d = 0; d <= 3; d++) {
     ents.clear();
     rval = mb->get_entities_by_dimension(0, d, ents);
-    if (moab::MB_SUCCESS != rval)
+    if (moab::MB_SUCCESS != rval) {
+      delete mb;
       return 1;
+    }
     std::cout << "Found " << ents.size() << " " << d << "-dimensional entities:" << std::endl;
     for (moab::Range::iterator it = ents.begin(); it != ents.end(); it++) {
       moab::EntityHandle ent = *it;

diff --git a/src/io/ReadNCDF.cpp b/src/io/ReadNCDF.cpp
index 3ed25d1..c400cd2 100644
--- a/src/io/ReadNCDF.cpp
+++ b/src/io/ReadNCDF.cpp
@@ -171,13 +171,11 @@ void ReadNCDF::reset()
     nodesInLoadedBlocks.clear();
 }
 
-
 ReadNCDF::~ReadNCDF() 
 {
   mdbImpl->release_interface(readMeshIface);
 }
   
-
 ErrorCode ReadNCDF::read_tag_values(const char* file_name,
                                     const char* tag_name,
                                     const FileOptions& ,
@@ -236,8 +234,6 @@ ErrorCode ReadNCDF::read_tag_values(const char* file_name,
   return MB_SUCCESS;
 }
 
-
-
 ErrorCode ReadNCDF::load_file(const char *exodus_file_name,
                               const EntityHandle* file_set,
                               const FileOptions& opts,
@@ -328,8 +324,6 @@ ErrorCode ReadNCDF::load_file(const char *exodus_file_name,
   return MB_SUCCESS;
 }
 
-
-
 ErrorCode ReadNCDF::read_exodus_header()
 {
   CPU_WORD_SIZE = sizeof(double);  // With ExodusII version 2, all floats
@@ -403,17 +397,18 @@ ErrorCode ReadNCDF::read_exodus_header()
     readMeshIface->report_error("ReadNCDF:: Problem getting title attribute.");
     return MB_FAILURE;
   }
-  char *title = new char[att_len+1];
   if (att_type != NC_CHAR) {
     readMeshIface->report_error("ReadNCDF:: title didn't have type char.");
     return MB_FAILURE;
   }
+  char *title = new char[att_len + 1];
   fail = nc_get_att_text(ncFile, NC_GLOBAL, "title", title);
   if (NC_NOERR != fail) {
     readMeshIface->report_error("ReadNCDF:: trouble getting title.");
+    delete[] title;
     return MB_FAILURE;
   }
-  delete [] title;
+  delete[] title;
 
   return MB_SUCCESS;
 }
@@ -683,12 +678,10 @@ ErrorCode ReadNCDF::read_elements(const Tag* file_id_tag)
     const int* reorder = exodus_elem_order_map[mb_type][verts_per_element];
     if (reorder)
       ReadUtilIface::reorder( reorder, conn, this_it->numElements, verts_per_element );
-    
 
-      
     readMeshIface->update_adjacencies((*this_it).startMBId, (*this_it).numElements,
                                       ExoIIUtil::VerticesPerElement[(*this_it).elemType], conn);
-    
+
     if ( result == -1 )
     {
       readMeshIface->report_error("ReadNCDF:: error getting element connectivity for block %i",
@@ -701,8 +694,7 @@ ErrorCode ReadNCDF::read_elements(const Tag* file_id_tag)
       return MB_FAILURE;
     if( mdbImpl->tag_set_data( mGlobalIdTag, &ms_handle, 1, &block_id ) != MB_SUCCESS )
       return MB_FAILURE;
-      
-      
+
     if (file_id_tag) {
       Range range;
       range.insert( this_it->startMBId, this_it->startMBId + this_it->numElements - 1 );
@@ -1199,7 +1191,6 @@ ErrorCode ReadNCDF::create_ss_elements( int *element_ids,
     else if( type == MBQUAD &&
              exoii_type >= EXOII_SHELL && exoii_type <= EXOII_SHELL9 )
     {
-
       //ent_handle = CREATE_HANDLE(MBQUAD, base_id, error );
 
       //just use this quad
@@ -1325,9 +1316,7 @@ ErrorCode ReadNCDF::create_ss_elements( int *element_ids,
             dist_factor_vector.push_back( temp_dist_factor_vector[df_index++] );
         }
       }
-
     }
-
   }
 
   return MB_SUCCESS; 
@@ -1411,11 +1400,9 @@ ErrorCode ReadNCDF::create_sideset_element( const std::vector<EntityHandle>& con
   return error;
 }
 
-
 ErrorCode ReadNCDF::find_side_element_type( const int exodus_id, ExoIIElementType &elem_type, 
                                                 ReadBlockData &block_data, int &df_index, int side_id)
 {
-
   std::vector<ReadBlockData>::iterator iter, end_iter;
   iter = blocksLoading.begin();
   end_iter = blocksLoading.end();
@@ -1450,7 +1437,6 @@ ErrorCode ReadNCDF::find_side_element_type( const int exodus_id, ExoIIElementTyp
           df_index += 3;
 
         return MB_FAILURE;
-
       }
 
       block_data = *iter;

diff --git a/src/io/WriteSmf.cpp b/src/io/WriteSmf.cpp
index 9d7f5f0..d2c4fc9 100644
--- a/src/io/WriteSmf.cpp
+++ b/src/io/WriteSmf.cpp
@@ -13,7 +13,6 @@
  * 
  */
 
-
 #ifdef WIN32
 #ifdef _DEBUG
 // turn off warnings that say they debugging identifier has been truncated
@@ -22,7 +21,6 @@
 #endif
 #endif
 
-
 #include "WriteSmf.hpp"
 
 #include <fstream>
@@ -48,15 +46,16 @@ namespace moab {
 const int DEFAULT_PRECISION = 10;
 //const bool DEFAULT_STRICT = true;
 
-WriterIface *WriteSmf::factory( Interface* iface )
-  { return new WriteSmf( iface ); }
+WriterIface *WriteSmf::factory(Interface* iface)
+{
+  return new WriteSmf(iface);
+}
 
-WriteSmf::WriteSmf(Interface *impl) 
-    : mbImpl(impl), writeTool(0)
+WriteSmf::WriteSmf(Interface *impl)
+  : mbImpl(impl), writeTool(0)
 {
   assert(impl != NULL);
-
-  impl->query_interface( writeTool );
+  impl->query_interface(writeTool);
 }
 
 WriteSmf::~WriteSmf() 
@@ -72,119 +71,124 @@ ErrorCode WriteSmf::write_file(const char *file_name,
                                  const std::vector<std::string>& ,
                                  const Tag* /*tag_list*/,
                                  int /*num_tags*/,
-                                 int )
+                                 int /*export_dimension*/)
 {
   ErrorCode rval;
 
-    // Get precision for node coordinates
+  // Get precision for node coordinates
   int precision;
-  if (MB_SUCCESS != opts.get_int_option( "PRECISION", precision ))
+  if (MB_SUCCESS != opts.get_int_option("PRECISION", precision))
     precision = DEFAULT_PRECISION;
-  
-   // Honor overwrite flag
-  if (!overwrite)
-  {
-    rval = writeTool->check_doesnt_exist( file_name );
+
+  // Honor overwrite flag
+  if (!overwrite) {
+    rval = writeTool->check_doesnt_exist(file_name);
     if (MB_SUCCESS != rval)
       return rval;
   }
-  
-    // Create file
-  std::ofstream file( file_name );
-  if (!file)
-  {
-    writeTool->report_error("Could not open file: %s\n", file_name );
+
+  // Create file
+  std::ofstream file(file_name);
+  if (!file) {
+    writeTool->report_error("Could not open file: %s\n", file_name);
     return MB_FILE_WRITE_ERROR;
   }
-  file.precision( precision );
-    // Get entities to write
-  
+  file.precision(precision);
+
+  // Get entities to write
   Range triangles;
-  if (!output_list || !num_sets)
-  {
-    rval = mbImpl->get_entities_by_type( 0, MBTRI, triangles, false);
-    if (MB_SUCCESS != rval) return rval;
+  if (!output_list || !num_sets) {
+    rval = mbImpl->get_entities_by_type(0, MBTRI, triangles, false);
+    if (MB_SUCCESS != rval)
+      return rval;
 
-    // somehow get all the nodes from this range, order them, uniquify, then use binary search
+  // Somehow get all the nodes from this range, order them, uniquify, then use binary search
   }
-  else
-  {
+  else {
     // get all triangles from output sets
-    for (int i=0; i<num_sets; i++)
-      rval = mbImpl->get_entities_by_type( output_list[i], MBTRI, triangles, false);
+    for (int i = 0; i < num_sets; i++)
+      rval = mbImpl->get_entities_by_type(output_list[i], MBTRI, triangles, false);
   }
-  // use an array with all the connectivities in the triangles; it will be converted later to ints
+  // Use an array with all the connectivities in the triangles; it will be converted later to ints
   int numTriangles = triangles.size();
-  int array_alloc = 3*numTriangles;       // allocated size of 'array'
+  int array_alloc = 3 * numTriangles;       // allocated size of 'array'
   EntityHandle* array = new EntityHandle[array_alloc]; // ptr to working array of result handles
-  // fill up array with node handles; reorder and uniquify 
+  // Fill up array with node handles; reorder and uniquify
   if (!array)
      return MB_MEMORY_ALLOCATION_FAILED;
   int fillA = 0;
-  for (Range::const_iterator e = triangles.begin(); e != triangles.end(); ++e)
-    {
+  for (Range::const_iterator e = triangles.begin(); e != triangles.end(); ++e) {
       const EntityHandle* conn;
       int conn_len;
-      rval = mbImpl->get_connectivity( *e, conn, conn_len );
-      if (MB_SUCCESS != rval  ) 
-	return rval;
-      if ( 3!=conn_len) 
+      rval = mbImpl->get_connectivity(*e, conn, conn_len);
+      if (MB_SUCCESS != rval) {
+        delete[] array;
+        return rval;
+      }
+      if (3 != conn_len) {
+        delete[] array;
         return MB_INVALID_SIZE;
+      }
 
       for (int i = 0; i < conn_len; ++i)
         array[fillA++] = conn[i];
-    }
-  if (fillA != array_alloc)
-	 return MB_INVALID_SIZE;
-    
-  std::sort( array, array + array_alloc);
-  int numNodes = std::unique(array, array + array_alloc ) - array;
-  
+  }
+  if (fillA != array_alloc) {
+    delete[] array;
+    return MB_INVALID_SIZE;
+  }
+
+  std::sort(array, array + array_alloc);
+  int numNodes = std::unique(array, array + array_alloc) - array;
+
   file << "#$SMF 1.0\n";
   file << "#$vertices " << numNodes << std::endl;
   file << "#$faces " << numTriangles << std::endl;
   file << "# \n";
   file << "# output from MOAB \n";
   file << "# \n";
-  
+
   // output first the nodes
   // num nodes??
   // write the nodes 
   double coord[3];
-  for(int i=0; i<numNodes; i++)
-    {
-      EntityHandle node_handle = array[i];
-     
-      rval = mbImpl->get_coords(&node_handle,1, coord);
-      if(rval !=MB_SUCCESS) return rval;
-      
-      file << "v " << coord[0] << " " << coord[1] << " " << coord[2] << std::endl; 
+  for (int i = 0; i < numNodes; i++) {
+    EntityHandle node_handle = array[i];
+
+    rval = mbImpl->get_coords(&node_handle, 1, coord);
+    if (rval != MB_SUCCESS) {
+      delete[] array;
+      return rval;
     }
-  // write faces now
-  // leave a blank line for cosmetics
+
+    file << "v " << coord[0] << " " << coord[1] << " " << coord[2] << std::endl;
+  }
+  // Write faces now
+  // Leave a blank line for cosmetics
   file << " \n";
-  for (Range::const_iterator e = triangles.begin(); e != triangles.end(); ++e)
-    {
-      const EntityHandle* conn;
-      int conn_len;
-      rval = mbImpl->get_connectivity( *e, conn, conn_len );
-      if (MB_SUCCESS != rval  ) 
-	return rval;
-      if ( 3!=conn_len) 
-        return MB_INVALID_SIZE;
-      file << "f ";
-      for (int i = 0; i < conn_len; ++i)
-      {
-	int indexInArray = std::lower_bound( array, array + numNodes, conn[i] ) - array;
-        file << indexInArray + 1 << " " ;
-      }
-      file << std::endl;
+  for (Range::const_iterator e = triangles.begin(); e != triangles.end(); ++e) {
+    const EntityHandle* conn;
+    int conn_len;
+    rval = mbImpl->get_connectivity(*e, conn, conn_len);
+    if (MB_SUCCESS != rval) {
+      delete[] array;
+      return rval;
     }
+    if (3!= conn_len) {
+      delete[] array;
+      return MB_INVALID_SIZE;
+    }
+    file << "f ";
+    for (int i = 0; i < conn_len; ++i) {
+      int indexInArray = std::lower_bound(array, array + numNodes, conn[i]) - array;
+      file << indexInArray + 1 << " ";
+    }
+    file << std::endl;
+  }
 
   file.close();
-  delete [] array;
+  delete[] array;
   return MB_SUCCESS;
 }
 
 } // namespace moab
-

diff --git a/src/parallel/ParallelComm.cpp b/src/parallel/ParallelComm.cpp
index 2d9e61c..d9e06d5 100644
--- a/src/parallel/ParallelComm.cpp
+++ b/src/parallel/ParallelComm.cpp
@@ -614,6 +614,10 @@ namespace moab {
       
         result = pack_buffer(entities[i], adjacencies, tags, 
                              false, -1, &buff); 
+        if (MB_SUCCESS != result) {
+          delete[] sendCounts;
+          delete[] displacements;
+        }
         RRA("Failed to compute buffer size in scatter_entities.");
 
         buff_size = buff.buff_ptr - buff.mem_ptr - prev_size;
@@ -626,6 +630,8 @@ namespace moab {
     success = MPI_Bcast(sendCounts, nProcs, MPI_INT, from_proc, procConfig.proc_comm());
     if (MPI_SUCCESS != success) {
       result = MB_FAILURE;
+      delete[] sendCounts;
+      delete[] displacements;
       RRA("MPI_Bcast of buffer size failed.");
     }
   
@@ -643,6 +649,8 @@ namespace moab {
   
     if (MPI_SUCCESS != success) {
       result = MB_FAILURE;
+      delete[] sendCounts;
+      delete[] displacements;
       RRA("MPI_Scatterv of buffer failed.");
     }
 
@@ -655,10 +663,17 @@ namespace moab {
       rec_buff.reset_ptr(sizeof(int));
       result = unpack_buffer(rec_buff.buff_ptr, false, from_proc, -1, 
                              dum1a, dum1b, dum1p, dum2, dum2, dum3, dum4);
+      if (MB_SUCCESS != result) {
+        delete[] sendCounts;
+        delete[] displacements;
+      }
       RRA("Failed to unpack buffer in scatter_entities.");
       std::copy(dum4.begin(), dum4.end(), range_inserter(entities[my_proc]));
     }
 
+    delete[] sendCounts;
+    delete[] displacements;
+
     return MB_SUCCESS;
 #endif
   }

Repository URL: https://bitbucket.org/fathomteam/moab/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.


More information about the moab-dev mailing list