From 57cab6b34632a1cb3933cdaee4ffa5b6f823fb16 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Wed, 4 Dec 2019 18:44:38 -0500 Subject: [PATCH 1/2] Address some minor Valgrind errors/warnings --- include/openmc/material.h | 3 ++- include/openmc/tallies/filter.h | 3 ++- include/openmc/tallies/tally.h | 2 +- src/mesh.cpp | 7 +++++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/include/openmc/material.h b/include/openmc/material.h index 652f3db8e..7ce845fc0 100644 --- a/include/openmc/material.h +++ b/include/openmc/material.h @@ -11,6 +11,7 @@ #include "pugixml.hpp" #include "xtensor/xtensor.hpp" +#include "openmc/constants.h" #include "openmc/bremsstrahlung.h" #include "openmc/particle.h" @@ -132,7 +133,7 @@ public: //---------------------------------------------------------------------------- // Data - int32_t id_; //!< Unique ID + int32_t id_ {C_NONE}; //!< Unique ID std::string name_; //!< Name of material std::vector nuclide_; //!< Indices in nuclides vector std::vector element_; //!< Indices in elements vector diff --git a/include/openmc/tallies/filter.h b/include/openmc/tallies/filter.h index ba6ca0883..7c46a24c2 100644 --- a/include/openmc/tallies/filter.h +++ b/include/openmc/tallies/filter.h @@ -9,6 +9,7 @@ #include +#include "openmc/constants.h" #include "openmc/hdf5_interface.h" #include "openmc/particle.h" #include "pugixml.hpp" @@ -118,7 +119,7 @@ public: protected: int n_bins_; private: - int32_t id_ {-1}; + int32_t id_ {C_NONE}; gsl::index index_; }; diff --git a/include/openmc/tallies/tally.h b/include/openmc/tallies/tally.h index 2a166738d..70a282222 100644 --- a/include/openmc/tallies/tally.h +++ b/include/openmc/tallies/tally.h @@ -69,7 +69,7 @@ public: //---------------------------------------------------------------------------- // Major public data members. - int id_; //!< User-defined identifier + int id_ {C_NONE}; //!< User-defined identifier std::string name_; //!< User-defined name diff --git a/src/mesh.cpp b/src/mesh.cpp index ac02b679e..fcdc815cb 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -3,6 +3,7 @@ #include // for copy, equal, min, min_element #include // for size_t #include // for ceil +#include // for allocator #include #ifdef OPENMC_MPI @@ -803,9 +804,11 @@ RegularMesh::count_sites(const std::vector& bank, cnt(mesh_bin) += site.wgt; } - // Create copy of count data + // Create copy of count data. Since ownership will be acquired by xtensor, + // std::allocator must be used to avoid Valgrind mismatched free() / delete + // warnings. int total = cnt.size(); - double* cnt_reduced = new double[total]; + double* cnt_reduced = std::allocator{}.allocate(total); #ifdef OPENMC_MPI // collect values from all processors From ede575ea041b01b31973f871256df8fc8202e969 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Wed, 4 Dec 2019 18:56:52 -0500 Subject: [PATCH 2/2] Make more use of C_NONE for un-initalized ids --- src/material.cpp | 8 ++++---- src/tallies/filter.cpp | 8 ++++---- src/tallies/tally.cpp | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/material.cpp b/src/material.cpp index a5bc3dc13..38eef82a7 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -857,12 +857,12 @@ void Material::calculate_photon_xs(Particle& p) const void Material::set_id(int32_t id) { - Expects(id >= -1); + Expects(id >= 0 || id == C_NONE); // Clear entry in material map if an ID was already assigned before - if (id_ != -1) { + if (id_ != C_NONE) { model::material_map.erase(id_); - id_ = -1; + id_ = C_NONE; } // Make sure no other material has same ID @@ -871,7 +871,7 @@ void Material::set_id(int32_t id) } // If no ID specified, auto-assign next ID in sequence - if (id == -1) { + if (id == C_NONE) { id = 0; for (const auto& m : model::materials) { id = std::max(id, m->id_); diff --git a/src/tallies/filter.cpp b/src/tallies/filter.cpp index c67eec830..d8fe58cc7 100644 --- a/src/tallies/filter.cpp +++ b/src/tallies/filter.cpp @@ -148,12 +148,12 @@ Filter* Filter::create(const std::string& type, int32_t id) void Filter::set_id(int32_t id) { - Expects(id >= -1); + Expects(id >= 0 || id == C_NONE); // Clear entry in filter map if an ID was already assigned before - if (id_ != -1) { + if (id_ != C_NONE) { model::filter_map.erase(id_); - id_ = -1; + id_ = C_NONE; } // Make sure no other filter has same ID @@ -162,7 +162,7 @@ void Filter::set_id(int32_t id) } // If no ID specified, auto-assign next ID in sequence - if (id == -1) { + if (id == C_NONE) { id = 0; for (const auto& f : model::tally_filters) { id = std::max(id, f->id_); diff --git a/src/tallies/tally.cpp b/src/tallies/tally.cpp index 852918a0c..059803e76 100644 --- a/src/tallies/tally.cpp +++ b/src/tallies/tally.cpp @@ -483,12 +483,12 @@ Tally::create(int32_t id) void Tally::set_id(int32_t id) { - Expects(id >= -1); + Expects(id >= 0 || id == C_NONE); // Clear entry in tally map if an ID was already assigned before - if (id_ != -1) { + if (id_ != C_NONE) { model::tally_map.erase(id_); - id_ = -1; + id_ = C_NONE; } // Make sure no other tally has the same ID @@ -497,7 +497,7 @@ Tally::set_id(int32_t id) } // If no ID specified, auto-assign next ID in sequence - if (id == -1) { + if (id == C_NONE) { id = 0; for (const auto& t : model::tallies) { id = std::max(id, t->id_);