Skip to content

Commit

Permalink
HDF5: Make sure datasets are always closed upon reading, even when si…
Browse files Browse the repository at this point in the history
…ze 0 data is read.

HDF5MeasDir should return if all files were closed properly.
  • Loading branch information
KerstinKeller committed Oct 9, 2023
1 parent 154b809 commit d03e0f6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
10 changes: 6 additions & 4 deletions contrib/ecalhdf5/src/eh5_meas_dir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,28 @@ bool eCAL::eh5::HDF5MeasDir::Open(const std::string& path, eAccessType access /*

bool eCAL::eh5::HDF5MeasDir::Close()
{
bool successfully_closed{ true };

if (access_ == eAccessType::CREATE)
{
// Close all existing file writers
for (auto& file_writer : file_writers_)
{
file_writer.second->Close();
successfully_closed &= file_writer.second->Close();
}

// Clear the list of all file writers, which will delete them
file_writers_.clear();

return true;
return successfully_closed;
}
else
{
for (auto file : file_readers_)
{
if (file != nullptr)
{
file->Close();
successfully_closed &= file->Close();
delete file;
file = nullptr;
}
Expand All @@ -125,7 +127,7 @@ bool eCAL::eh5::HDF5MeasDir::Close()
entries_by_id_.clear();
entries_by_chn_.clear();

return true;
return successfully_closed;
}
}

Expand Down
10 changes: 6 additions & 4 deletions contrib/ecalhdf5/src/eh5_meas_file_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,15 @@ bool eCAL::eh5::HDF5MeasFileV2::GetEntryData(long long entry_id, void* data) con

auto size = H5Dget_storage_size(dataset_id);

if (size <= 0) return false;

auto readStatus = H5Dread(dataset_id, H5T_NATIVE_UCHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
herr_t read_status = -1;
if (size >= 0)
{
read_status = H5Dread(dataset_id, H5T_NATIVE_UCHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
}

H5Dclose(dataset_id);

return (readStatus >= 0);
return (read_status >= 0);
}


Expand Down
2 changes: 1 addition & 1 deletion testing/contrib/ecalhdf5/hdf5_test/src/hdf5_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ TEST(HDF5, WriteReadIntegrity)
const long long t1_clock = 11LL;

const std::string t2_name = "another,topic";
const std::string t2_data = "Data of topic 2";
const std::string t2_data = "";
const long long t2_snd_timestamp = 1002LL;
const long long t2_rcv_timestamp = 2002LL;
const long long t2_id = 2LL;
Expand Down

0 comments on commit d03e0f6

Please sign in to comment.