From 5dc68fb665912adf0d10bfa134db7c91f7b3de58 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Tue, 5 Mar 2019 11:22:53 -0500 Subject: [PATCH] Fix memory errors caught by Valgrind --- include/openmc/hdf5_interface.h | 9 +++------ src/nuclide.cpp | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/include/openmc/hdf5_interface.h b/include/openmc/hdf5_interface.h index e2662b71e5..0be98718ce 100644 --- a/include/openmc/hdf5_interface.h +++ b/include/openmc/hdf5_interface.h @@ -282,17 +282,14 @@ void read_dataset(hid_t dset, xt::xarray& arr, bool indep=false) // Get shape of dataset std::vector shape = object_shape(dset); - // Allocate new array to read data into + // Allocate space in the array to read data into std::size_t size = 1; for (const auto x : shape) size *= x; - T* buffer = new T[size]; + arr.resize(shape); // Read data from attribute - read_dataset(dset, nullptr, H5TypeMap::type_id, buffer, indep); - - // Adapt into xarray - arr = xt::adapt(buffer, size, xt::acquire_ownership(), shape); + read_dataset(dset, nullptr, H5TypeMap::type_id, arr.data(), indep); } template<> diff --git a/src/nuclide.cpp b/src/nuclide.cpp index 236833acea..cd2461186a 100644 --- a/src/nuclide.cpp +++ b/src/nuclide.cpp @@ -413,7 +413,7 @@ void Nuclide::init_grid() while (std::log(grid.energy[j + 1]/E_min) <= umesh(k)) { // Ensure that for isotopes where maxval(grid.energy) << E_max that // there are no out-of-bounds issues. - if (j + 1 == grid.energy.size()) break; + if (j + 2 == grid.energy.size()) break; ++j; } grid.grid_index[k] = j;