[cig-commits] [commit] baagaard/feature-output-station-names: Fixed memory leak. (d71b5a0)

cig_noreply at geodynamics.org cig_noreply at geodynamics.org
Tue Dec 2 17:37:50 PST 2014


Repository : https://github.com/geodynamics/pylith

On branch  : baagaard/feature-output-station-names
Link       : https://github.com/geodynamics/pylith/compare/9e23a4c06bbae6322f76e6f093dfe0b09b5f1839...d71b5a06992a5e77fef79839b166f2fe665cca67

>---------------------------------------------------------------

commit d71b5a06992a5e77fef79839b166f2fe665cca67
Author: Brad Aagaard <baagaard at usgs.gov>
Date:   Tue Dec 2 17:37:25 2014 -0800

    Fixed memory leak.


>---------------------------------------------------------------

d71b5a06992a5e77fef79839b166f2fe665cca67
 libsrc/pylith/meshio/HDF5.cc | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/libsrc/pylith/meshio/HDF5.cc b/libsrc/pylith/meshio/HDF5.cc
index d05f5a4..62135e7 100644
--- a/libsrc/pylith/meshio/HDF5.cc
+++ b/libsrc/pylith/meshio/HDF5.cc
@@ -1140,6 +1140,7 @@ pylith::meshio::HDF5::writeDataset(hid_t h5,
   assert(nstrings > 0);
   assert(slen > 0);
 
+  char* strfixedlen = 0;
   try {
     // Open group
 #if defined(PYLITH_HDF5_USE_API_18)
@@ -1164,7 +1165,7 @@ pylith::meshio::HDF5::writeDataset(hid_t h5,
     if (err < 0) 
       throw std::runtime_error("Could not set size of datatype.");
 
-    char* strfixedlen = (nstrings*slen > 0) ? new char[nstrings*slen] : 0;
+    strfixedlen = (nstrings*slen > 0) ? new char[nstrings*slen] : 0;
     for (int i=0; i < nstrings; ++i) {
       const int index = i*slen;
       strncpy(&strfixedlen[index], sarray[i], slen-1);
@@ -1183,6 +1184,7 @@ pylith::meshio::HDF5::writeDataset(hid_t h5,
       throw std::runtime_error("Could not create dataset.");
 
     err = H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, strfixedlen);
+    delete[] strfixedlen; strfixedlen = 0;
     if (err < 0)
       throw std::runtime_error("Could not write dataset.");
 
@@ -1203,12 +1205,16 @@ pylith::meshio::HDF5::writeDataset(hid_t h5,
       throw std::runtime_error("Could not close group.");
 
   } catch (const std::exception& err) {
+    delete[] strfixedlen; strfixedlen = 0;
+
     std::ostringstream msg;
     msg << "Error occurred while creating dataset '"
 	<< parent << "/" << name << "':\n"
 	<< err.what();
     throw std::runtime_error(msg.str());
   } catch (...) {
+    delete[] strfixedlen; strfixedlen = 0;
+
     std::ostringstream msg;
     msg << "Unknown error occurred while writing dataset '" << name << "'.";
     throw std::runtime_error(msg.str());



More information about the CIG-COMMITS mailing list