HDF5: Add cleaner way of handling object destruction (explicit Close() for file and dataset, all destructors have HDF5 TRY blocks)
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 11m9s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 9m57s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 9m17s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 10m26s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 10m58s
Build Packages / build:rpm (rocky8) (push) Successful in 13m10s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 15m9s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 11m44s
Build Packages / build:rpm (rocky9) (push) Successful in 13m56s
Build Packages / Generate python client (push) Successful in 31s
Build Packages / Build documentation (push) Successful in 56s
Build Packages / Create release (push) Has been skipped
Build Packages / build:rpm (ubuntu2404) (push) Successful in 9m30s
Build Packages / XDS test (durin plugin) (push) Successful in 7m55s
Build Packages / XDS test (JFJoch plugin) (push) Successful in 7m32s
Build Packages / DIALS test (push) Successful in 11m58s
Build Packages / XDS test (neggia plugin) (push) Successful in 7m16s
Build Packages / Unit tests (push) Failing after 1h18m18s

This commit is contained in:
2026-05-05 13:28:41 +02:00
parent 829cad0847
commit 5407e3e189
2 changed files with 88 additions and 8 deletions
+86 -8
View File
@@ -94,7 +94,12 @@ void HDF5DataSpace::SelectHyperslabWithStride(const std::vector<hsize_t> &start,
}
HDF5DataSpace::~HDF5DataSpace() {
if (id >= 0) H5Sclose(id);
if (id >= 0) {
H5E_BEGIN_TRY {
H5Sclose(id);
} H5E_END_TRY;
id = -1;
}
}
uint8_t HDF5DataSpace::GetNumOfDimensions() const {
@@ -208,7 +213,12 @@ HDF5DataType::HDF5DataType(const HDF5DataSet &data_set) :HDF5Id() {
}
HDF5DataType::~HDF5DataType() {
if (id >= 0) H5Tclose(id);
if (id >= 0) {
H5E_BEGIN_TRY {
H5Tclose(id);
} H5E_END_TRY;
id = -1;
}
}
size_t HDF5DataType::GetElemSize() const {
@@ -258,7 +268,12 @@ HDF5Dcpl::HDF5Dcpl(const HDF5DataSet &data_set) : HDF5Id() {
}
HDF5Dcpl::~HDF5Dcpl() {
if (id >= 0) H5Pclose(id);
if (id >= 0) {
H5E_BEGIN_TRY {
H5Pclose(id);
} H5E_END_TRY;
id = -1;
}
}
void HDF5Dcpl::SetChunking(const std::vector<hsize_t> &dims) {
@@ -321,7 +336,12 @@ HDF5Fapl::HDF5Fapl() : HDF5Id() {
}
HDF5Fapl::~HDF5Fapl() {
H5Pclose(id);
if (id >= 0) {
H5E_BEGIN_TRY {
H5Pclose(id);
} H5E_END_TRY;
id = -1;
}
}
void HDF5Fapl::SetVersionTo1p10orNewer() {
@@ -698,7 +718,12 @@ HDF5Group::HDF5Group(const HDF5Object& parent, const char *name) : HDF5Object()
}
HDF5Group::~HDF5Group() {
H5Gclose(id);
if (id >= 0) {
H5E_BEGIN_TRY {
H5Gclose(id);
} H5E_END_TRY;
id = -1;
}
}
HDF5File::HDF5File(const std::string& filename, bool v1_10) : HDF5Object() {
@@ -711,8 +736,37 @@ HDF5File::HDF5File(const std::string& filename, bool v1_10) : HDF5Object() {
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot open/create data HDF5 file " + filename);
}
void HDF5File::Close() {
if (id < 0)
return;
// Invalidate first; if anything below fails (e.g. ENOSPC) we must NOT
// leave a live id behind for the destructor or later code to touch.
const hid_t local_id = id;
id = -1;
herr_t flush_err = 0;
H5E_BEGIN_TRY {
flush_err = H5Fflush(local_id, H5F_SCOPE_GLOBAL);
} H5E_END_TRY;
herr_t close_err = 0;
H5E_BEGIN_TRY {
close_err = H5Fclose(local_id);
} H5E_END_TRY;
if (flush_err < 0 || close_err < 0)
throw JFJochException(JFJochExceptionCategory::HDF5,
"Failed to flush/close HDF5 file (likely no space left on device)");
}
HDF5File::~HDF5File() {
if (id >= 0) H5Fclose(id);
if (id >= 0) {
H5E_BEGIN_TRY {
H5Fclose(id);
} H5E_END_TRY;
id = -1;
}
}
void HDF5File::Delete(const std::string& path) {
@@ -726,7 +780,10 @@ HDF5ReadOnlyFile::HDF5ReadOnlyFile(const std::string &filename) {
}
HDF5ReadOnlyFile::~HDF5ReadOnlyFile() {
if (id >= 0) H5Fclose(id);
if (id >= 0) {
H5E_BEGIN_TRY {H5Fclose(id); } H5E_END_TRY;
id = -1;
}
}
HDF5DataSet::HDF5DataSet(const HDF5Object &parent, const std::string &name, const HDF5DataType &data_type,
@@ -812,8 +869,29 @@ std::string HDF5DataSet::ReadString() const {
return buffer;
}
void HDF5DataSet::Close() {
if (id < 0)
return;
const hid_t local_id = id;
id = -1;
herr_t err = 0;
H5E_BEGIN_TRY {
err = H5Dclose(local_id);
} H5E_END_TRY;
if (err < 0)
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot close HDF5 dataset");
}
HDF5DataSet::~HDF5DataSet() {
if (id >= 0) H5Dclose(id);
if (id >= 0) {
H5E_BEGIN_TRY {
H5Dclose(id);
} H5E_END_TRY;
id = -1;
}
}
void HDF5DataSet::ReadDirectChunk(std::vector<uint8_t> &val, const std::vector<hsize_t> &offset) {
+2
View File
@@ -196,6 +196,7 @@ public:
explicit HDF5File(const std::string& filename, bool v1_10 = false);
~HDF5File();
void Delete(const std::string& path);
void Close();
};
class HDF5ReadOnlyFile : public HDF5Object {
@@ -348,6 +349,7 @@ public:
}
std::string ReadString() const;
void Close();
};
inline std::unique_ptr<HDF5DataSet> SaveScalar(const HDF5Object& parent, const std::string &name, const char* val) {