[MOAB-dev] commit/MOAB: danwu: Fix memory leaks for some unit test files.

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Tue Sep 24 13:21:06 CDT 2013


1 new commit in MOAB:

https://bitbucket.org/fathomteam/moab/commits/9d73aa1918de/
Changeset:   9d73aa1918de
Branch:      master
User:        danwu
Date:        2013-09-24 20:20:54
Summary:     Fix memory leaks for some unit test files.

Affected #:  4 files

diff --git a/test/h5file/h5file_test.cpp b/test/h5file/h5file_test.cpp
index 66703a5..94dc218 100644
--- a/test/h5file/h5file_test.cpp
+++ b/test/h5file/h5file_test.cpp
@@ -42,7 +42,6 @@ const int REGION_SET_ID = 1103;
 const int EMPTY_SET_ID  = 1100;
 const int SET_SET_ID    = 1105;
 
-
 Interface* iface;
 
 void create();
@@ -83,7 +82,6 @@ int main(int argc, char* argv[])
     }
   }
   
-  
   iface = new Core();
   
   // create a dodecahedron and inscribed hex
@@ -98,6 +96,7 @@ int main(int argc, char* argv[])
     fprintf( stderr, "Failed to write \"%s\"\n", filename );
     if (MB_SUCCESS == iface->get_last_error( msg ))
       fprintf( stderr, "%s\n", msg.c_str() );
+    delete iface;
     return 1;
   }
   
@@ -109,6 +108,7 @@ int main(int argc, char* argv[])
     fprintf( stderr, "Failed to read \"%s\"\n", filename );
     if (MB_SUCCESS == iface->get_last_error( msg ))
       fprintf( stderr, "%s\n", msg.c_str() );
+    delete iface;
     return 1;
   }
   
@@ -117,6 +117,7 @@ int main(int argc, char* argv[])
   if (!compare())
   {
     fprintf(stderr, "Comparison failed.\n");
+    delete iface;
     return 1;
   }
   fprintf( stderr, "success!\n" );
@@ -129,6 +130,7 @@ int main(int argc, char* argv[])
     fprintf( stderr, "Failed to write \"%s\"\n", filename );
     if (MB_SUCCESS == iface->get_last_error( msg ))
       fprintf( stderr, "%s\n", msg.c_str() );
+    delete iface;
     return 1;
   }
  
@@ -146,6 +148,7 @@ int main(int argc, char* argv[])
     fprintf( stderr, "Failed to read \"%s\"\n", filename );
     if (MB_SUCCESS == iface->get_last_error( msg ))
       fprintf( stderr, "%s\n", msg.c_str() );
+    delete iface;
     return 1;
   }
 
@@ -154,6 +157,7 @@ int main(int argc, char* argv[])
   if (!compare())
   {
     fprintf(stderr, "Comparison failed.\n");
+    delete iface;
     return 1;
   }
   fprintf( stderr, "success!\n" );
@@ -167,6 +171,7 @@ int main(int argc, char* argv[])
   // Clean up the file.
   remove( filename );
   fprintf( stderr, "done.\n" );
+  delete iface;
   return 0;
 }
 
@@ -233,7 +238,6 @@ EntityHandle make_set( unsigned int options,
   return handle;
 }
 
-
 void create()
 {
   // Create dodecahedron
@@ -427,8 +431,7 @@ bool compare_conn( std::vector<EntityHandle>& conn1,
       return false;
     }
   }
- 
-  
+
   std::vector<int> tags[2];
   tags[0].resize( conn1.size() ); tags[1].resize( conn2.size() );
   Tag tag;
@@ -486,9 +489,8 @@ bool compare_sets( int id, const char* tag_name = 0 )
   EntityHandle set1 = *range.begin();
   EntityHandle set2 = *++range.begin();
   
-  
     // Compare set descriptions
-  
+
   unsigned opt1, opt2;
   if (MB_SUCCESS != iface->get_meshset_options( set1, opt1 ) ||
       MB_SUCCESS != iface->get_meshset_options( set2, opt2 ))
@@ -509,7 +511,6 @@ bool compare_sets( int id, const char* tag_name = 0 )
     return false;
   }
 
-
     // Compare set contents
     // First check if same number of entities.
     // Then select from three possible methods to compare set contents
@@ -599,10 +600,9 @@ bool compare_sets( int id, const char* tag_name = 0 )
     if (!ok)
       return false;
   }
-  
-    
+
     // Compare set parent/child links using global id
-  
+
   ok = true;
   const char* words[] = { "children", "parents" };
   std::vector<EntityHandle> adj1, adj2;
@@ -686,7 +686,6 @@ bool compare_tags( EntityHandle dod[] )
     return false;
   }
 
-
     // Get double tag handle and characterstics
   if (MB_SUCCESS != iface->tag_get_handle( dblname, 1, MB_TYPE_DOUBLE, tag, MB_TAG_DENSE|MB_TAG_STORE ))
     moab_error( "tag_get_handle(dblname)" );
@@ -708,7 +707,6 @@ bool compare_tags( EntityHandle dod[] )
     return false;
   }
  
-
     // Get handle tag handle and characterstics
   if (MB_SUCCESS != iface->tag_get_handle( handlename, 3, MB_TYPE_HANDLE, tag, MB_TAG_SPARSE|MB_TAG_STORE ))
     moab_error( "tag_get_handle(handlename)" );
@@ -760,8 +758,6 @@ bool compare_tags( EntityHandle dod[] )
   
   return true;
 }
- 
-  
 
 bool compare()
 {
@@ -861,6 +857,6 @@ void moab_error( const char* str )
   fprintf( stderr, "%s() failed.\n", str );
   if (MB_SUCCESS == iface->get_last_error( msg ))
     fprintf( stderr, "%s\n", msg.c_str() );
+  delete iface;
   exit( 1 );
 }
-  

diff --git a/test/io/VtkTest.cpp b/test/io/VtkTest.cpp
index 100674c..d6208a2 100644
--- a/test/io/VtkTest.cpp
+++ b/test/io/VtkTest.cpp
@@ -108,7 +108,7 @@ DECLARE_TEST(unstructured_field)
 
 int main( int argc, char* argv[] )
 {
-  int *test_indices = (int*)malloc( sizeof(int) * num_tests );
+  int *test_indices = (int*)malloc(sizeof(int) * num_tests);
   int test_count;
     // if no arguments, do all tests
   if (argc == 1) {
@@ -126,8 +126,7 @@ int main( int argc, char* argv[] )
   }
   
   int fail_count = 0;
-  for (int i = 0; i < test_count; ++i)
-  {
+  for (int i = 0; i < test_count; ++i) {
     test_data& test = test_array[test_indices[i]];
     printf("Testing %s...\n", test.name );
     if (!(test.result = test.test()))
@@ -137,23 +136,23 @@ int main( int argc, char* argv[] )
   printf("\n\n");
   if (fail_count) {
     printf("FAILED TESTS:\n");
-    for (int i = 0; i < test_count; ++i)
-    {
+    for (int i = 0; i < test_count; ++i) {
       test_data& test = test_array[test_indices[i]];
       if (!test.result)
         printf("\t%s\n", test.name);
     }
   }
-    
-  
+
   if (test_count == 0)
     printf("0 VTK tests run\n");
   else if (fail_count == 0)
     printf("%d tests passed\n", test_count );
   else
-    printf("%d of %d tests failed\n", fail_count, test_count );
+    printf("%d of %d tests failed\n", fail_count, test_count);
   printf("\n");
-  
+
+  free(test_indices);
+
   return fail_count;
 }
 
@@ -179,7 +178,6 @@ static inline bool is_error( ErrorCode b )
 bool read_file( Interface* iface, const char* file );
 bool write_and_read( Interface* iface1, Interface* iface2 );
 
-
 bool test_read_write_element( const double* coords, unsigned num_coords,
                               const int* vtk_conn, const int* moab_conn,
                               unsigned num_conn,
@@ -203,8 +201,7 @@ bool test_edge2()
       
   return test_read_write_element( coords, 5, conn, conn, 10, 5, 3, MBEDGE );
 }
-  
-  
+
 bool test_edge3()
 {
   const double coords[] =
@@ -243,7 +240,6 @@ bool test_tri3()
   return test_read_write_element( coords, 5, conn, conn, 12, 4, 5, MBTRI );
 }
 
-
 bool test_tri6()
 {
   const double coords[] = 
@@ -429,7 +425,6 @@ bool test_tet10()
   return test_read_write_element( coords, 14, conn, conn, 20, 2, 24, MBTET );
 }
 
-
 const double grid_2x2x2[] =
   { 0, 0, 0,
     1, 0, 0,
@@ -904,7 +899,6 @@ bool test_vector_attrib_double()
   return test_vector_attrib("double", MB_TYPE_DOUBLE);
 }
 
-
 bool test_tensor_attrib_uchar()
 {
   return test_tensor_attrib("unsigned_char", MB_TYPE_INTEGER);
@@ -955,9 +949,6 @@ bool test_tensor_attrib_double()
   return test_tensor_attrib("double", MB_TYPE_DOUBLE);
 }
 
-
-
-
 bool read_file( Interface* iface, const char* file )
 {
   char fname[] = "tmp_file.vtk";
@@ -1086,7 +1077,6 @@ bool match_vertices_and_elements( Interface* iface,
   return true;
 }
 
-
 bool check_elements( Interface* iface,
                      EntityType moab_type, unsigned num_elem, unsigned vert_per_elem,
                      const double* coords, unsigned num_vert, 
@@ -1099,8 +1089,6 @@ bool check_elements( Interface* iface,
   CHECK(rval);
   return true;
 }
-      
-                     
 
 bool test_read_write_element( const double* coords, unsigned num_verts,
                               const int* vtk_conn, const int* moab_conn,
@@ -1295,7 +1283,6 @@ bool check_tag_values( Interface* iface,
   return true;
 }
 
-
 bool check_tag_values( Interface* iface, DataType type, int vals_per_ent )
 {
   EntityHandle vert_handles[6], elem_handles[2];
@@ -1369,7 +1356,7 @@ bool test_tensor_attrib( const char* vtk_type, DataType mb_type )
   return check_tag_data( file, mb_type, 9 );
 } 
 
-bool test_subset( )
+bool test_subset()
 {
   Core moab_inst;
   Interface& moab = moab_inst;

diff --git a/test/kd_tree_time.cpp b/test/kd_tree_time.cpp
index 10af975..af0e33d 100644
--- a/test/kd_tree_time.cpp
+++ b/test/kd_tree_time.cpp
@@ -8,49 +8,49 @@
 
 using namespace moab;
 
-void usage( const char* argv0 )
+void usage(const char* argv0)
 {
   fprintf(stderr, "usage: %s [-t] [-d <result_file>] <tree_file><point_file> [<count>]\n", argv0);
   exit(1);
 }
 
-void print_file_stats( Interface& moab )
+void print_file_stats(Interface& moab)
 {
   ErrorCode rval;
   int num_tri;
   Range sets;
   unsigned long set_mem, set_am, tag_mem, tag_am;
-  
-  rval = moab.get_number_entities_by_type( 0, MBTRI, num_tri );
+
+  rval = moab.get_number_entities_by_type(0, MBTRI, num_tri);
   if (MB_SUCCESS != rval)
     num_tri = -1;
-  rval = moab.get_entities_by_type( 0, MBENTITYSET, sets );
+  rval = moab.get_entities_by_type(0, MBENTITYSET, sets);
   if (MB_SUCCESS != rval)
     sets.clear();
-  
-  moab.estimated_memory_use( sets, 0, 0, &set_mem, &set_am, 0, 0, 0, 0, &tag_mem, &tag_am );
-  printf( "Triangles:   %d\n", num_tri );
-  printf( "Sets:        %lu\n", (unsigned long)sets.size() );
-  printf( "Set storage: %lu (%lu)\n", set_mem, set_am );
-  printf( "Tag storage: %lu (%lu)\n", tag_mem, tag_am );
+
+  moab.estimated_memory_use(sets, 0, 0, &set_mem, &set_am, 0, 0, 0, 0, &tag_mem, &tag_am);
+  printf("Triangles:   %d\n", num_tri);
+  printf("Sets:        %lu\n", (unsigned long)sets.size());
+  printf("Set storage: %lu (%lu)\n", set_mem, set_am);
+  printf("Tag storage: %lu (%lu)\n", tag_mem, tag_am);
 }
 
-int main( int argc, char* argv[] ) 
+int main(int argc, char* argv[])
 {
   double* values;
   unsigned long length;
   FILE *file, *rfile = 0;
   unsigned long count = 0;
   clock_t t;
-  
+
   const char* tree_file = 0;
   const char* point_file = 0;
   const char* result_file = 0;
   bool query_triangles = false;
-  
+
   if (argc < 3 || argc > 7)
     usage(argv[0]);
-  
+
   for (int i = 1; i < argc; ++i) {
     if (!strcmp("-t", argv[i]))
       query_triangles = true;
@@ -66,87 +66,93 @@ int main( int argc, char* argv[] )
       point_file = argv[i];
     else {
       char* endptr;
-      count = strtol( argv[i], &endptr, 0 );
+      count = strtol(argv[i], &endptr, 0);
       if (*endptr || count < 1)
         usage(argv[0]);
     }
   }
-  
-  file = fopen( point_file, "rb" );
+
+  file = fopen(point_file, "rb");
   if (!file) {
     perror(point_file);
     return 2;
   }
-  fseek( file, 0, SEEK_END );
-  length = ftell(file) / (3*sizeof(double));
-  fseek( file, 0, SEEK_SET );
-  values = new double[3*length];
-  if (length != fread( values, 3*sizeof(double), length, file )) {
-    fprintf( stderr, "Error reading %lu points from file \"%s\"\n", length, argv[2] );
+  fseek(file, 0, SEEK_END);
+  length = ftell(file) / (3 * sizeof(double));
+  fseek(file, 0, SEEK_SET);
+  values = new double[3 * length];
+  if (length != fread(values, 3 * sizeof(double), length, file)) {
+    fprintf(stderr, "Error reading %lu points from file \"%s\"\n", length, argv[2]);
+    delete[] values;
     return 2;
   }
-  fclose( file );
-  
+  fclose(file);
+
   if (result_file) {
-    rfile = fopen( result_file, "w" );
+    rfile = fopen(result_file, "w");
     if (!rfile) {
       perror(result_file);
+      delete[] values;
       return 2;
     }
   }
-  
+
   if (!count)
     count = length;
- 
-  printf("Loading tree..."); fflush( stdout );
+
+  printf("Loading tree...");
+  fflush(stdout);
   t = clock();
   Core moab;
-  ErrorCode rval = moab.load_mesh( tree_file, 0, 0 );
+  ErrorCode rval = moab.load_mesh(tree_file, 0, 0);
   if (MB_SUCCESS != rval) {
-    fprintf(stderr,"Failed to read file: %s\n", tree_file );
+    fprintf(stderr,"Failed to read file: %s\n", tree_file);
+    delete[] values;
     return 2;
   }
-  printf("%0.2f seconds\n", (clock()-t)/(double)CLOCKS_PER_SEC); fflush( stdout );
-  
+  printf("%0.2f seconds\n", (clock() - t) / (double)CLOCKS_PER_SEC);
+  fflush(stdout);
+
   Range range;
   AdaptiveKDTree tool(&moab);
   tool.find_all_trees(range);
   if (range.size() != 1) {
-    fprintf(stderr,"%s : found %d kd-trees\n", argv[1], (int)range.size() );
+    fprintf(stderr,"%s : found %d kd-trees\n", argv[1], (int)range.size());
+    delete[] values;
     return 3;
   }
   EntityHandle root = range.front();
 
-  print_file_stats( moab );
-  
-  printf("Running point queries..."); fflush( stdout );
+  print_file_stats(moab);
+
+  printf("Running point queries...");
+  fflush(stdout);
   t = clock();
   EntityHandle leaf;
   double pt[3];
   for (unsigned long i = 0; i < count; ++i) {
     const double* coords = values + 3 * (i % length);
     if (query_triangles)
-      rval = tool.closest_triangle( root, coords, pt, leaf );
+      rval = tool.closest_triangle(root, coords, pt, leaf);
     else
       rval = tool.point_search(coords, leaf, 0.0, NULL, &root);
     if (MB_SUCCESS != rval) {
       fprintf(stderr, "Failure (ErrorCode == %d) for point %d (%f, %f, %f)\n",
-        (int)rval, (int)(count % length), coords[0], coords[1], coords[2] );
+        (int)rval, (int)(count % length), coords[0], coords[1], coords[2]);
     }
     else if (rfile) {
       if (query_triangles)
-        fprintf( rfile, "%f %f %f %f %f %f %lu\n", 
+        fprintf(rfile, "%f %f %f %f %f %f %lu\n",
           coords[0], coords[1], coords[2],
-          pt[0], pt[1], pt[2], (unsigned long)leaf );
+          pt[0], pt[1], pt[2], (unsigned long)leaf);
       else
-        fprintf( rfile, "%f %f %f %lu\n", 
-          coords[0], coords[1], coords[2], (unsigned long)leaf );
+        fprintf(rfile, "%f %f %f %lu\n",
+          coords[0], coords[1], coords[2], (unsigned long)leaf);
     }
   }
-  printf("%0.2f seconds\n", (clock()-t)/(double)CLOCKS_PER_SEC); fflush( stdout );
+  printf("%0.2f seconds\n", (clock() - t) / (double)CLOCKS_PER_SEC);
+  fflush(stdout);
   delete[] values;
 
   return 0;
 }
-
-  

diff --git a/test/parallel/scdtest.cpp b/test/parallel/scdtest.cpp
index 5fe6f36..3202621 100644
--- a/test/parallel/scdtest.cpp
+++ b/test/parallel/scdtest.cpp
@@ -47,37 +47,37 @@ int main(int argc, char *argv[])
   MPI_Init(&argc, &argv);
   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
   MPI_Comm_size(MPI_COMM_WORLD, &size);
-  if(size != 4 && size != 2) {
+  if (size != 4 && size != 2) {
     cerr << "Run this with 2 or 4 processes\n";
     exit(1);
   }
-  
+
   mbint = new Core();
-  mbpc  = new ParallelComm(mbint, MPI_COMM_WORLD);
+  mbpc = new ParallelComm(mbint, MPI_COMM_WORLD);
 
   set_local_domain_bounds();
   create_hexes_and_verts();
   resolve_and_exchange();
 
+  delete mbpc; // ParallelComm instance should be deleted before MOAB instance is deleted
+  delete mbint;
+
   MPI_Finalize();
   return 0;
 }
 
-
 void set_local_domain_bounds() 
 {
   switch (size) {
     case 2:
-        switch(rank) {
+        switch (rank) {
           case 0:
-              is = 0; ie = NI/2;
+              is = 0; ie = NI / 2;
               js = 0; je = NJ;
               ks = 0; ke = NK;
               break;
-
-
           case 1:
-              is = NI/2; ie = NI;
+              is = NI / 2; ie = NI;
               js = 0; je = NJ;
               ks = 0; ke = NK;
               break;
@@ -85,25 +85,25 @@ void set_local_domain_bounds()
         break;
         
     case 4:
-        switch(rank) {
+        switch (rank) {
           case 0:
-              is = 0; ie = NI/2;
-              js = 0; je = NJ/2;
+              is = 0; ie = NI / 2;
+              js = 0; je = NJ / 2;
               ks = 0; ke = NK;
               break;
           case 1:
-              is = NI/2; ie = NI;
-              js = 0; je = NJ/2;
+              is = NI / 2; ie = NI;
+              js = 0; je = NJ / 2;
               ks = 0; ke = NK;
               break;
           case 2:
-              is = 0; ie = NI/2;
-              js = NJ/2; je = NJ;
+              is = 0; ie = NI / 2;
+              js = NJ / 2; je = NJ;
               ks = 0; ke = NK;
               break;
           case 3:
-              is = NI/2; ie = NI;
-              js = NJ/2; je = NJ;
+              is = NI / 2; ie = NI;
+              js = NJ / 2; je = NJ;
               ks = 0; ke = NK;
               break;
         }
@@ -115,12 +115,11 @@ void set_local_domain_bounds()
   }
 }
 
-
 void create_hexes_and_verts()
 {
   Core *mbcore = dynamic_cast<Core*>(mbint);
   HomCoord coord_min(0,0,0);
-  HomCoord coord_max(ie-is, je-js, ke-ks);
+  HomCoord coord_max(ie - is, je - js, ke - ks);
   EntitySequence* vertex_seq = NULL;
   EntitySequence* cell_seq = NULL;
   EntityHandle vs, cs;
@@ -139,32 +138,31 @@ void create_hexes_and_verts()
   Tag global_id_tag;
   error(mbint->tag_get_handle(GLOBAL_ID_TAG_NAME, 1, MB_TYPE_INTEGER, global_id_tag));
   EntityHandle handle = vs;
-  int i,j,k;
+  int i, j, k;
 
   ErrorCode err;
 
-  for(k = ks; k < ke + 1; k++)
-    for(j = js; j < je + 1; j++)
-      for(i = is; i < ie + 1; i++) {
-        gid = 1 + i + j*(NI+1) + k*(NI+1)*(NJ+1);
+  for (k = ks; k < ke + 1; k++)
+    for (j = js; j < je + 1; j++)
+      for (i = is; i < ie + 1; i++) {
+        gid = 1 + i + j*(NI + 1) + k*(NI + 1)*(NJ + 1);
         err = mbint->tag_set_data(global_id_tag, &handle, 1, &gid);
-        if(err != MB_SUCCESS) {
+        if (err != MB_SUCCESS) {
           exit(1);
         }
         handle++;
       }
 
   handle = cs;
-  for(k = ks; k < ke; k++)
-    for(j = js; j < je; j++)
-      for(i = is; i < ie; i++) {
+  for (k = ks; k < ke; k++)
+    for (j = js; j < je; j++)
+      for (i = is; i < ie; i++) {
         gid = 1 + i + j*NI + k*NI*NJ;
         error(mbint->tag_set_data(global_id_tag, &handle, 1, &gid));
-	handle++;
+        handle++;
       }
 }
 
-
 void resolve_and_exchange()
 {
   EntityHandle entity_set;
@@ -198,7 +196,7 @@ void resolve_and_exchange()
 
 void error(ErrorCode err)
 {
-  if(err != MB_SUCCESS) {
+  if (err != MB_SUCCESS) {
     cerr << "Error: MOAB function failed\n";
     assert(0);
   }

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