@@ -389,6 +389,8 @@ void HDF5IOHandlerImpl::createPath(
389389 " [HDF5] Creating a path in a file opened as read only is not "
390390 " possible." );
391391
392+ herr_t status = 0 ;
393+
392394 hid_t gapl = H5Pcreate (H5P_GROUP_ACCESS);
393395#if H5_VERSION_GE(1, 10, 0) && openPMD_HAVE_MPI
394396 if (m_hdf5_collective_metadata)
@@ -397,7 +399,15 @@ void HDF5IOHandlerImpl::createPath(
397399 }
398400#endif
399401
400- herr_t status = 0 ;
402+ auto defer_close_gapl = auxiliary::defer ([&]() {
403+ status = H5Pclose (gapl);
404+ if (status != 0 )
405+ {
406+ std::cerr << " [HDF5] Internal error: Failed to close HDF5 property "
407+ " during path creation."
408+ << std::endl;
409+ }
410+ });
401411
402412 if (!writable->written )
403413 {
@@ -468,16 +478,6 @@ void HDF5IOHandlerImpl::createPath(
468478
469479 m_fileNames[writable] = file.name ;
470480 }
471-
472- auto defer_close_gapl = auxiliary::defer ([&]() {
473- status = H5Pclose (gapl);
474- if (status != 0 )
475- {
476- std::cerr << " [HDF5] Internal error: Failed to close HDF5 property "
477- " during path creation."
478- << std::endl;
479- }
480- });
481481}
482482
483483namespace
@@ -968,7 +968,7 @@ void HDF5IOHandlerImpl::createDataset(
968968 // > should be able to detect and recycle the file space when no
969969 // > other reference to the deleted object exists
970970 // https://github.com/openPMD/openPMD-api/pull/1007#discussion_r867223316
971- herr_t status = H5Ldelete (node_id, name.c_str (), H5P_DEFAULT);
971+ status = H5Ldelete (node_id, name.c_str (), H5P_DEFAULT);
972972 VERIFY (
973973 status == 0 ,
974974 " [HDF5] Internal error: Failed to delete old dataset '" +
@@ -1053,7 +1053,7 @@ void HDF5IOHandlerImpl::createDataset(
10531053 }
10541054 else
10551055 {
1056- herr_t status = H5Pset_chunk (
1056+ status = H5Pset_chunk (
10571057 datasetCreationProperty,
10581058 chunking->size (),
10591059 chunking->data ());
@@ -1066,7 +1066,7 @@ void HDF5IOHandlerImpl::createDataset(
10661066
10671067 for (auto const &filter : filters)
10681068 {
1069- herr_t status = std::visit (
1069+ status = std::visit (
10701070 auxiliary::overloaded{
10711071 [&](DatasetParams::ByID const &by_id) {
10721072 return H5Pset_filter (
@@ -1527,7 +1527,6 @@ void HDF5IOHandlerImpl::openDataset(
15271527
15281528 hid_t dataset_type, dataset_space;
15291529 dataset_type = H5Dget_type (dataset_id);
1530- dataset_space = H5Dget_space (dataset_id);
15311530 auto defer_close_dataset_type = auxiliary::defer ([&]() {
15321531 status = H5Tclose (dataset_type);
15331532 if (status != 0 )
@@ -1538,6 +1537,8 @@ void HDF5IOHandlerImpl::openDataset(
15381537 << std::endl;
15391538 }
15401539 });
1540+
1541+ dataset_space = H5Dget_space (dataset_id);
15411542 auto defer_close_dataset_space = auxiliary::defer ([&]() {
15421543 status = H5Sclose (dataset_space);
15431544 if (status != 0 )
@@ -1625,11 +1626,18 @@ void HDF5IOHandlerImpl::openDataset(
16251626 " HDF5" ,
16261627 " Unknown dataset type" );
16271628 };
1629+
16281630 if (remaining_tries == 0 )
16291631 {
16301632 throw_error ();
16311633 }
1634+
16321635 hid_t next_type = H5Tget_super (dataset_type);
1636+ if (next_type == H5I_INVALID_HID)
1637+ {
1638+ throw_error ();
1639+ }
1640+
16331641 auto defer_close_next_type = auxiliary::defer ([&]() {
16341642 status = H5Tclose (next_type);
16351643 if (status != 0 )
@@ -1640,29 +1648,15 @@ void HDF5IOHandlerImpl::openDataset(
16401648 << std::endl;
16411649 }
16421650 });
1643- if (next_type == H5I_INVALID_HID)
1644- {
1645- throw_error ();
1646- }
1647- else if (H5Tequal (dataset_type, next_type))
1651+
1652+ if (H5Tequal (dataset_type, next_type))
16481653 {
16491654 throw_error ();
16501655 }
1651- else
1652- {
1653- {
1654- throw error::ReadError (
1655- error::AffectedObject::Group,
1656- error::Reason::Other,
1657- " HDF5" ,
1658- " Internal error: Failed to close HDF5 dataset type "
1659- " during "
1660- " dataset opening" );
1661- }
1662- dataset_type = next_type;
1663- --remaining_tries;
1664- repeat = true ;
1665- }
1656+
1657+ dataset_type = next_type;
1658+ --remaining_tries;
1659+ repeat = true ;
16661660 }
16671661 } while (repeat);
16681662 }
@@ -2143,7 +2137,6 @@ void HDF5IOHandlerImpl::writeAttribute(
21432137 " [HDF5] Internal error: Failed to create HDF5 attribute during "
21442138 " attribute write" );
21452139 };
2146- bool created_new_attribute = false ;
21472140 if (H5Aexists (node_id, name.c_str ()) != 0 )
21482141 {
21492142 attribute_id = H5Aopen (node_id, name.c_str (), H5P_DEFAULT);
@@ -2179,30 +2172,22 @@ void HDF5IOHandlerImpl::writeAttribute(
21792172 " attribute "
21802173 " during attribute write" );
21812174 create_attribute_anew ();
2182- created_new_attribute = true ;
21832175 }
21842176 }
21852177 else
21862178 {
21872179 create_attribute_anew ();
2188- created_new_attribute = true ;
21892180 }
2190- auto defer_close_attribute_id =
2191- auxiliary::defer ([&]() {
2192- if (created_new_attribute || attribute_id >= 0 )
2193- {
2194- status = H5Aclose (attribute_id);
2195- if (status != 0 )
2196- {
2197- std::cerr
2198- << " [HDF5] Internal error: Failed to close attribute " +
2199- name + " at " +
2200- concrete_h5_file_position (writable) +
2201- " during attribute write."
2202- << std::endl;
2203- }
2204- }
2205- });
2181+ auto defer_close_attribute_id = auxiliary::defer ([&]() {
2182+ status = H5Aclose (attribute_id);
2183+ if (status != 0 )
2184+ {
2185+ std::cerr << " [HDF5] Internal error: Failed to close attribute " +
2186+ name + " at " + concrete_h5_file_position (writable) +
2187+ " during attribute write."
2188+ << std::endl;
2189+ }
2190+ });
22062191
22072192 using DT = Datatype;
22082193 switch (dtype)
0 commit comments