Writing a variable length strinf


I have a dataset defined with a compound datatype . One of the consitituents of tbe datatype is a variable length string which is defined as follows

     hid_t stringType = H5Tcopy(H5T_C_S1);


I am inserting this into the compound datatype as follows


Datastruct is a C struct wherein name is defined as char* .

For wirting into this dataset , I need to write one field at a time . In such a case I will have to create a compound datatype having a variable length string , which I doing as given below .

     hid_t stringtype = H5Tcopy(H5T_C_S1);

     H5Tset_size(stringtype, H5T_VARIABLE);

     hid_t strtype = H5Tcreate(H5T_COMPOUND, H5Tget_size(stringtype));

     status = H5Tinsert(strtype,"name", 0, stringtype);

   int length = feat.GetName().length(); // obtains the strimg to be written

     char *name = new char[length+1]; //length = 13



     status = H5Dwrite(dataset, strtype, memspace, dspace, H5P_DEFAULT, name);

This leads to the following error

HDF5: infinite loop closing library





I can understand that I cannot define the string length as H5T_VARIABLE the second time when I am actually writing the data .

But if I give the actual length I will get the error because src and dest datatypes will differ .

What is the correct way to handle this ?

Thanks and Regards