H5Awrite fails with error

Hello,

I am currently integrating the HDF5 library into our software with good success so far. However, I just can’t get H5Awrite to succeed. The following steps work as expected:

  • Create a new file.
  • Create and write to a dataset.
  • Add an attribute to the dataset.

It then fails when I try to write to the attribute. I can confirm that the attribute is successfully created, but it contents remains zero.

The shown error is:
HDF5-DIAG: Error detected in HDF5 (1.12.0) thread 0:
#000: …\externals\hdf5\src\src\H5A.c line 672 in H5Awrite(): not an attribute
major: Invalid arguments to routine
minor: Inappropriate type

My code roughly looks like this:

att = H5Acreate2(object.id(), attributename, datatype, dspace.id(), H5P_DEFAULT, H5P_DEFAULT);

if(att.id() < 0)
	return error::att_ioerror;

herr_t status = H5Awrite(att.id(), datatype, data);

if(status < 0)
	return error::att_ioerror;

object.id() = – the id of the “testdata” dataset –
attributename = “test_scalar”
datatype = H5T_IEEE_F32LE
dspace.id() = H5Screate(H5S_SCALAR);
data = – pointer to a single float –

The saved data looks like this:

Any sugestion would be welcome, I already invested a whole day into this and just cannot figure out what the actual problem is.

Thank you,
Daniel

suggests that att.id() does not return the identifier returned by H5Acreate2 (or any other attribute identifier for that matter).

G.

Thank you gheber, I took another look t it and realised that HAclose throws the same error.

However, the ID is correct. So I simply tried something obvious and open the attribute and the result it this:

hid_t aa = H5Acreate2(object.id(), attributename, datatype, dspace.id(), H5P_DEFAULT, H5P_DEFAULT);

hid_t ab = H5Aopen(object.id(), attributename, H5P_DEFAULT);

aa = 504403158265495552
ab = 504403158265495553

So opening the attribute returns the next id after creating the attribute. Is this expected?

What is even stranger, if I call:

herr_t status = H5Awrite(aa, datatype, data);

This works, although it is the ID from H5Acreate2. When I use the ID of H5Aopen, it does not work.
H4Aclose fails either way.

I should mention that I do build from source code.

Thank you,
Daniel

Okay I found my problem. I use a class to ensure the attribute is closed before the function returns. For whatever reason, the compiled code destroys this object too early, before the write function. I restructured my code to make it clearer to the compiler when the attribute ID is used and now it works, although it is basically the same code.

Thank you gheber for your suggestion.

Daniel