Given the following function, I am trying to write a variable length string to an open HDF5 file. However, the H5Awrite(...) line causes an access violation inside the HDF5 stack, which I can't see. Before I go find or build debug libraries for HDF5, I was hoping there was some obvious issue.
Also, I removed the error handling that would normally be there to make the code more simple while debugging and sharing. I inspected and verified each hid_t is valid, and the HDF5 library has not issued any prior warnings or errors.
#include <hdf5.h>
#include <string>
void writeAttr(hid_t parent, const std::string& name, const std::string& value)
{
hid_t type = H5Tcopy(H5T_C_S1);
H5Tset_size(type, H5T_VARIABLE);
hid_t attr = H5Acreate(parent, name.c_str(), type, H5Screate(H5S_SCALAR), H5P_DEFAULT, H5P_DEFAULT);
herr_t status = H5Awrite(attr, type, value.c_str()); // <-- access violation
H5Aclose(attr);
H5Tclose(type);
}
int main()
{
const std::string filename = "D:\\test.h5";
hid_t file = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
writeAttr(file, "Attribute", "Attribute value");
H5Fclose(file);
return 0;
}
I have also tried adding the following with no luck:
H5Tset_cset(type, H5T_CSET_ASCII);
H5Tset_strpad(type, H5T_STR_NULLTERM);
HDF5 library version 1.14.1
H5Acreatemay return a negative value to indicate error. Also,H5T_VARIABLEshouldn't be needed since you alread know the length of the string. I'd replaceH5T_VARIABLEwithvalue.size().