Access violation writing string attributes using VC++ 2005 + HDF5 1.8.5

This is probably my fault for using VC++ 2005, but that's the compiler we're
stuck with until our project migrates to 2008 ....we may have some 3rd party
vendor issues that prevent us moving to 2010 :frowning:

The application is as follows:
An application uses a C++ extension dll.
The C++ extension dll uses HDF5 1.8.5 release build, which in turn sits on
top of MSVC RTL version 9.

first, some defines and inlines ...
#define TLF_MAX_ATTRIBUTE_STRLEN 250
#define TMS_HDF_VLSTR_VAR(varname) H5::StrType varname(0,H5T_VARIABLE)
#define TMS_HDF_STR_VAR(varname) H5::StrType varname(0,
TLF_MAX_ATTRIBUTE_STRLEN)

note that I have 2 ways of declaring a string type.

//an inline to write general attributes, creating them if necessary.
inline void writeAttr(hid_t objId, const H5::AtomType & localVar, const
H5::DataSpace & attSpace, const char * attName, void * pBuff)
{
  hid_t attr_id = H5Aopen(objId, attName, H5P_DEFAULT);
  if (attr_id < 0) {
    attr_id = H5Acreate(objId, attName, localVar.getId(), attSpace.getId(),
H5P_DEFAULT,H5P_DEFAULT);
  }
  herr_t atterr = H5Awrite(attr_id, localVar.getId(), pBuff);
  H5Aclose(attr_id);
}

//and an inline to write STRING attributes
inline void writeAttrStr(hid_t objId, const H5::DataSpace & attSpace, const
char * attName, const char * attVal)
{
  char szAttVal[TLF_MAX_ATTRIBUTE_STRLEN];
  strcpy_s(szAttVal,attVal);
  try {
    //TMS_HDF_VLSTR_VAR(localVar);//this ALWAYS causes an exception at HDF
1.8.5 / VSTUDIO 2005
    TMS_HDF_STR_VAR(localVar);
    writeAttr(objId,localVar, attSpace, attName, szAttVal);//note szAttVal not
attVal
  }
  catch (...) {
    //you'll need to declare a global dbgCount and ref in this file as extern
to get
    //the counter in the instrumentation below, it's commented out for now.
    std::cerr << std::endl << "writeAttrStr err # " /*<< ++dbgCount*/ << " at:
" << getHdfPath(objId) << " attrName/Val: " << attName << "/" << (const
char*)szAttVal << std::endl;
  }
}

Some background: the "attVal" const char * argument is passed in as the
result of a std::string c_str() call, e.g.
std::string myAtt("whatever");
std::string myAttVal("blob");
writeAttrString(someObjId,someDataSpace,myAtt.c_str(),myAttVal.c_str());

Which should be legitimate (you can assume someObjId and someDataSpace are
valid).

Using macro TMS_HDF_VLSTR_VAR ALWAYS results in an AV in the RTL: every
time, whether I pass attVal or szAttVal to the writeAttr call.

Curiously, if I use macro TMS_HDF_STR_VAR, and pass attVal to writeAttr,
there is no problem for the first 1700 writes, then an RTL AV exception is
thrown. Then it's ok for the next few hundred, then another AV... there
doesn't seem to be a pattern to this, my instrumented code from my app shows
exceptions occurring at call numbers
1723
2149
2548
2589
3450
3717
4511

Suspecting the RTL, I inserted the strcpy_s to szAttVal and pass szAttVal
instead. Works a treat, no exceptions are thrown. Very odd. Anyone else come
across this? Anyone come across the AV with variable length string
attributes? If so, does this go away with later versions of VC++?

···

--
View this message in context: http://hdf-forum.184993.n3.nabble.com/Access-violation-writing-string-attributes-using-VC-2005-HDF5-1-8-5-tp1785421p1785421.html
Sent from the hdf-forum mailing list archive at Nabble.com.