Search code examples
chdf5

Freeing memory allocated by hdf5 library


I'm running a c library that dynamically allocates memory and loads a string array from an h5 file.

I get an error when freeing memory after use, I'm not sure if it's a windows problem or I'm not doing something right.

For windows I use the Visual Studio C Compilers (2019) and the version for the hdf5 library is 1.12

{
    unsigned mode        = H5F_ACC_RDONLY;
    char     file_name[] = "d1.h5";

    // assume a priori knowledge of dataset name and size
    char dset_name[] = "/group_name/daset_name";
    char**  elts = calloc(10, sizeof(char*));

    hid_t file = H5Fopen(file_name, mode, H5P_DEFAULT));
    hid_t dset = H5Dopen2(file, dset_name, H5P_DEFAULT);
    hid_t h5_type = H5Tcopy(H5T_C_S1);
    herr_t ret = H5Tset_size(h5_type, H5T_VARIABLE);


    // read all dataset elements
    H5Dread(dset, h5_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, elts);

    H5Tclose(dset);
    H5Dclose(dset);
    H5Fclose(file);

    // elts would be something like
    //    "asdf"
    //    "asdff"
    //    "asdfwer"
    //    "asdfffasdf"
    //    "asd"
    //    "asdf"
    //    "asdff"
    //    "asdfwer"
    //    "asdfffasdf"
    //    "asd"


    // do something w/ the dataset elements


    // after working free memory
    for(size_t i = 0; i < 10; i++) {
        free(elts[i]); // fails on this line
    }
    free(elts); // this line does not fail

}

This code fails only on windows in debug mode, in release it works. The programs exits and the error is:

minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp(908): Assertion failed: is_block_type_valid(header->_block_use)
unknown file: error: SEH exception with code 0xc0000005 thrown in the test body

Solution

  • After understanding what this RFC mean. I found the documentation for the method available on the HDF5 library specifically for this purpose.

    As per documentation, most of the memory management falls on the user side the working with HDF5, that is when loading data from the file you usually allocate the memory first and the HDF5 fills this allocated memory with data.

    In some cases HDF5 allocates some of the memory (i.e. when working with string arrays) and the memory must be free by the same component that allocated it. Due to the specific case of the window C run time dlls some heap corruption problems arise. So a method to free this memory has been implemented in the HDF5 library since version 1.8.13.

    I'll link the docs to the method. HDF5_freememory()