Why does this code leak? (simple codesnippet)

Posted by Ela782 on Stack Overflow See other posts from Stack Overflow or by Ela782
Published on 2012-11-18T19:24:40Z Indexed on 2013/07/02 23:06 UTC
Read the original article Hit count: 187

Filed under:
|
|

Visual Studio shows me several leaks (a few hundred lines), in total more than a few MB. I traced it down to the following "helloWorld example". The leak disappears if I comment out the H5::DataSet.getSpace() line.

#include "stdafx.h"
#include <iostream>
#include "cpp/H5Cpp.h"

int main(int argc, char *argv[])
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); // dump leaks at return

H5::H5File myfile;
try {
    myfile = H5::H5File("C:\\Users\\yyy\\myfile.h5", H5F_ACC_RDONLY);
}
catch (H5::Exception& e) {
    std::string msg( std::string( "Could not open HDF5 file.\n" ) + e.getCDetailMsg() );
    throw msg;
}

H5::Group myGroup = myfile.openGroup("/so/me/group");
H5::DataSet myDS = myGroup.openDataSet("./myfloatvec");
hsize_t dims[1];
//myDS.getSpace().getSimpleExtentDims(dims, NULL); // <-- here's the leak

H5::DataSpace dsp = myDS.getSpace(); // The H5::DataSpace seems to leak
dsp.getSimpleExtentDims(dims, NULL);
//dsp.close(); // <-- doesn't help either

std::cout << "Dims: " << dims[0] << std::endl; // <-- Works as expected

return 0;
}

Any help would be appreciated. I've been on this for hours, I hate unclean code...

© Stack Overflow or respective owner

Related posts about c++

Related posts about memory-leaks