Skip to content

Commit bbeabed

Browse files
BUG FIX: Fix HDF5 memory leaks. (#1446)
Signed-off-by: Joey Kleingers <[email protected]>
1 parent 675b006 commit bbeabed

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/simplnx/Utilities/Parsing/HDF5/IO/ObjectIO.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ Result<std::string> ObjectIO::readStringAttribute(const std::string& attributeNa
254254
}
255255
}
256256
H5Aclose(attribId);
257+
H5Tclose(attrTypeId);
257258
return returnResult;
258259
}
259260

@@ -303,12 +304,13 @@ bool ObjectIO::hasAttribute(const std::string& attributeName) const
303304

304305
usize ObjectIO::getNumElementsInAttribute(hid_t attribId) const
305306
{
306-
size_t typeSize = H5Tget_size(H5Aget_type(attribId));
307+
hid_t attrType = H5Aget_type(attribId);
308+
size_t typeSize = H5Tget_size(attrType);
307309
std::vector<hsize_t> dims;
308310
hid_t dataspaceId = H5Aget_space(attribId);
309311
if(dataspaceId >= 0)
310312
{
311-
Type type = getTypeFromId(H5Aget_type(attribId));
313+
Type type = getTypeFromId(attrType);
312314
if(type == Type::string)
313315
{
314316
size_t rank = 1;
@@ -324,6 +326,8 @@ usize ObjectIO::getNumElementsInAttribute(hid_t attribId) const
324326
if(error < 0)
325327
{
326328
std::cout << "Error Getting Attribute dims" << std::endl;
329+
H5Sclose(dataspaceId);
330+
H5Tclose(attrType);
327331
return 0;
328332
}
329333
// Copy the dimensions into the dims vector
@@ -332,6 +336,8 @@ usize ObjectIO::getNumElementsInAttribute(hid_t attribId) const
332336
std::copy(hdims.cbegin(), hdims.cend(), dims.begin());
333337
}
334338
}
339+
H5Sclose(dataspaceId);
340+
H5Tclose(attrType);
335341

336342
hsize_t numElements = std::accumulate(dims.cbegin(), dims.cend(), static_cast<hsize_t>(1), std::multiplies<hsize_t>());
337343
return numElements;

src/simplnx/Utilities/Parsing/HDF5/IO/ObjectIO.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ class SIMPLNX_EXPORT ObjectIO
182182

183183
herr_t error = H5Aread(attribId, typeId, values.data());
184184
H5Aclose(attribId);
185+
H5Tclose(typeId);
185186
if(error != 0)
186187
{
187188
std::string ss = fmt::format("Error Reading Vector Attribute '{}'.", attributeName);

0 commit comments

Comments
 (0)