Skip to content

memory leak as a result of using h5::open to create h5::pt_t #80

@ValentinKroner

Description

@ValentinKroner

As the title says, a tiny amount of memory is allocated and not released on using h5::open to get an instance of h5::pt_t.
The memory is not released when the pointer runs out of scope.

Minimal example code:

#include <iostream>
#include <thread>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wall"
#pragma GCC diagnostic ignored "-Wpedantic"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wreturn-type"
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wswitch"
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#include <h5cpp/all>
#pragma GCC diagnostic pop


int main(int ac, char *av[]) {

  (void) ac;
  (void) av;

  std::cout << "hello there" << std::endl;

  //Scope this entire thing to make sure everything is destroyed at the end
  {
    int n_calls = 10000000;
    unsigned int dim = 4;


    std::string file_path = "./test.hdf5";

    h5::fd_t handle = h5::create(file_path, H5F_ACC_EXCL);

    {
      h5::pt_t pt = h5::create<std::uint16_t>(
          handle, "test",
          h5::max_dims{H5S_UNLIMITED, dim, dim},
          h5::chunk{1, dim, dim});
    }

    for (int i = 0; i < n_calls; i++) {

      //this element is problematic, as a small bit of memory is retained even when the handle is destroyed at the
      //end of the for loop scope
      h5::pt_t pt = h5::open(handle, "test");

      std::vector<float> demo_frame_float = std::vector<float>(dim * dim, 76);

      h5::append(pt, demo_frame_float);
    }
  }

  std::cout << "done writing, time to sleep" << std::endl;

  //sleep to allow for inspection of system memory after the actual write
  std::this_thread::sleep_for(std::chrono::seconds(1000000));
}

The problem is not visible in valgrind, but can be observed in top under ubuntu when a lot of calls are performed.
In the the above code, the problem does not occur when the open call is lifted up from the for loop, and the pointer is reused.
OS version was 22.04, compiler was gcc12.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions