summaryrefslogtreecommitdiffstats
path: root/c++/src/H5Attribute.cpp
diff options
context:
space:
mode:
authorBinh-Minh Ribler <bmribler@hdfgroup.org>2008-04-20 22:06:37 (GMT)
committerBinh-Minh Ribler <bmribler@hdfgroup.org>2008-04-20 22:06:37 (GMT)
commit115689570f1a0802c3470cf69b61f32f486008f1 (patch)
tree07848498f64021e6bd9eebe04e5cd689d76d8736 /c++/src/H5Attribute.cpp
parenta0136eda8da079476621701b1147f5922ad9cfdb (diff)
downloadhdf5-115689570f1a0802c3470cf69b61f32f486008f1.zip
hdf5-115689570f1a0802c3470cf69b61f32f486008f1.tar.gz
hdf5-115689570f1a0802c3470cf69b61f32f486008f1.tar.bz2
[svn-r14850] Purpose: Fixed bugs
Description: - Revised Attribute::write and Attribute::read wrappers to handle memory allocation/deallocation properly. (bugzilla 1045) - Changed free() to H5Dfree(), also needed H5private.h - Corrected quite a few typos in documenting! Platforms tested: SunOS 5.10 (linew) Linux 2.6 (kagiso) FreeBSD (duty) - there was something wrong in the C tests for makecheck hung quite a long time; I went ahead with makecheck just in c++ dir, since the changes didn't effect the C tests. I'll keep an eye on the tests tonight...
Diffstat (limited to 'c++/src/H5Attribute.cpp')
-rw-r--r--c++/src/H5Attribute.cpp30
1 files changed, 22 insertions, 8 deletions
diff --git a/c++/src/H5Attribute.cpp b/c++/src/H5Attribute.cpp
index 1bca691..043dc4d 100644
--- a/c++/src/H5Attribute.cpp
+++ b/c++/src/H5Attribute.cpp
@@ -131,22 +131,36 @@ void Attribute::read( const DataType& mem_type, void *buf ) const
///\exception H5::AttributeIException
// Programmer Binh-Minh Ribler - Apr, 2003
// Modification
-// 2006/12/9 - H5Aread allocates memory for character string
-// buffer with malloc, therefore, no allocation here,
-// but HDfree is needed. - BMR
+// Mar 2008
+// Corrected a misunderstanding that H5Aread would allocate
+// space for the buffer. Obtained the attribute size and
+// allocated memory properly. - BMR
//--------------------------------------------------------------------------
void Attribute::read( const DataType& mem_type, H5std_string& strg ) const
{
- char* strg_C; // temporary C-string for C API
+ // Get the attribute size and allocate temporary C-string for C API
+ hsize_t attr_size = H5Aget_storage_size(id);
+ if (attr_size <= 0)
+ {
+ throw AttributeIException("Attribute::read", "Unable to get attribute size before reading");
+ }
+ char* strg_C = new char [attr_size+1];
+ if (strg_C == NULL)
+ {
+ throw AttributeIException("Attribute::read", "Unable to allocate buffer to read the attribute");
+ }
- // call C API to get the attribute string of chars
- herr_t ret_value = H5Aread( id, mem_type.getId(), &strg_C);
+ // Call C API to get the attribute data, a string of chars
+ herr_t ret_value = H5Aread(id, mem_type.getId(), &strg_C);
if( ret_value < 0 )
{
throw AttributeIException("Attribute::read", "H5Aread failed");
}
- strg = strg_C; // get 'string' from the C char*
- HDfree(strg_C);
+
+ // Get 'string' from the C char* and release resource
+ strg_C[attr_size] = '\0';
+ strg = strg_C;
+ delete []strg_C;
}
//--------------------------------------------------------------------------