Hi,
I am using HDF5 1.10.2 in a C++ project and I am trying to solve an issue when user selects a location with insufficient disk space. There is not an easy way to predict this will happen so I tried to simply catch errors (exceptions) during various operations and handle them. The problem is that when this happens, hdf5 crashes somewhere in destructor of H5::H5File. I have tried 1.12.2, but the issue is still there. Am I doing something wrong? Code to replicate the error follows:
#include <iostream>
#include <algorithm>
#include <filesystem>
#include <cstring>
#include <H5Cpp.h>
#include <windows.h>
int main()
{
// prepare everything
const std::string path = "O:/b.hdf5"; // virtual HDD with 100 MBs of space
const int desiredSize = 1024 * 1024 * 128;
std::shared_ptr<uint8_t> data = std::shared_ptr<uint8_t>(new uint8_t[desiredSize], std::default_delete<uint8_t[]>());
uint8_t* dataRaw = data.get();
for (int i = 0; i < desiredSize; ++i)
{
dataRaw[i] = i % 256;
}
// clean previous file
DeleteFileA(path.c_str());
// do HDF stuff
{
std::shared_ptr<H5::H5File> m_file;
// open file
try
{
m_file = std::make_shared<H5::H5File>(path.c_str(), H5F_ACC_RDWR);
}
catch (H5::Exception& error)
{
m_file = nullptr;
DeleteFileA(path.c_str());
}
if (m_file == nullptr)
{
try
{
m_file = std::make_shared<H5::H5File>(path.c_str(), H5F_ACC_TRUNC);
}
catch (H5::Exception& error)
{
m_file = nullptr;
DeleteFileA(path.c_str());
}
}
if (m_file == nullptr)
{
return EXIT_FAILURE;
}
// write data
{
std::shared_ptr<H5::Group> group;
try
{
group = std::shared_ptr<H5::Group>(new H5::Group(m_file->createGroup("H5::Group")));
}
catch (H5::Exception&)
{
group = nullptr;
}
if (group != nullptr)
{
std::shared_ptr<H5::DataSet> dataset;
try
{
hsize_t dimensions[1] = { desiredSize };
H5::DataSpace dataSpace(1, dimensions);
dataset = std::shared_ptr<H5::DataSet>(new H5::DataSet(group->createDataSet("H5::DataSet", H5::PredType::NATIVE_UINT8, dataSpace)));
dataset->write(static_cast<void*>(data.get()), H5::PredType::NATIVE_UINT8);
m_file->flush(H5F_SCOPE_GLOBAL);
}
catch (H5::Exception&)
{
bool damn = true;
}
}
}
// close
try
{
m_file->close();
}
catch (H5::Exception&)
{
bool damn = true;
}
m_file = nullptr;
}
return EXIT_SUCCESS;
}