2

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

20
  • Check that your calls succeeds. H5Acreate may return a negative value to indicate error. Also, H5T_VARIABLE shouldn't be needed since you alread know the length of the string. I'd replace H5T_VARIABLE with value.size(). Commented Jan 24 at 17:52
  • The C++ documentation for hdf5 looks really old. Is this library still maintained? Are you using the C API perhaps? Commented Jan 24 at 17:56
  • As stated in the question, I did inspect every variable and all are valid. All calls succeed until the access violation. As to putting in the size to make the string fixed sized, I am actually currently doing that, however, that has limitations later one during use, so I am trying to used variable length strings to circumvent those limitations. I am actually using the C interface, rather than the C++ because of historical reasons, which is why it looks old. However, the app is C++ (hence the strings). HDF5 is widely used, so I assume it is well maintained. Commented Jan 24 at 18:11
  • 1
    Hmmm. Fascinating. I wish it wasn't close to end of my work week. I am dying to dive into this more. Commented Jan 24 at 20:15
  • 1
    Well, I couldn't resist. So I downloaded the binaries from the HDF5 website for 1.14.5 (which I believe to be the latest), and the issue is still there. Commented Jan 24 at 20:36

1 Answer 1

3

I cheated and asked one of the maintainers who was kind enough to resolve the issue:

"Variable-length strings are a bit of a special case in HDF5. It's potentially a bit misleading due to the way the array is declared." ... "The short of it is essentially that the library expects char * for strings and char ** for variable-length strings.".

So:

auto lvptr = value.c_str();
herr_t status = H5Awrite(attr, type, &lvptr);

Then it runs without issues.

Sign up to request clarification or add additional context in comments.

4 Comments

That did the trick. Thanks for all the help! That was not clear in the documentation
@steveo225 You're welcome! No the doc could probably be clearer. jhendersonHDF, the maintainer that helped me, also pointed me at an example where variable length strings are used. Perhaps that could be useful too.
@steveo225 Would you say that the answer is acceptable in its current form or is there anything else you'd like me to add about this?
I think it is good. I was able to get this integrated immediately. I added a comment to so I don't forget why I had to that despite the docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.