c++cmemoryhdf5

does HDF5 resource need to be freed/closed before I use it again


I am writing an .h5 file in C++ and using some API calls as below. The HDF5 docs state that the identifier returned from these functions should be released to prevent leaks.

In the code I have, there are multiple calls of H5Dget_space() overwriting the identifier. My question is do I have to release the identifier before I make any subsequent calls again for the identifier. My intuition tells me its fine but I would like to be certain about it.

hid_t Acquisition = H5Gopen(fileHandle, "Acquisition/", H5P_DEFAULT);
hid_t gp0 = H5Gopen(Acquisition, "GP[0]", H5P_DEFAULT);
hid_t dset = H5Dopen(gp0, "RawData/", H5P_DEFAULT);

hid_t file_space = H5Dget_space(dset);

// ...

// Do I need to close `file_space` by `H5Sclose(file_space);` before reusing it in line below?
file_space = H5Dget_space(dset);

// ...

H5Sclose(file_space);
H5Dclose(dset);
H5Gclose(gp0);
H5Gclose(Acquisition);
H5Fclose(fileHandle);

Solution

  • You need to close each hid_t you create. That means each H5Dget_space() (except failed ones) must have a matching H5Sclose().