From 63dc0fa7e98ce8e7ca369c6f3fff59da295f50cb Mon Sep 17 00:00:00 2001 From: Michael Marshall <43034299+mmphys@users.noreply.github.com> Date: Thu, 4 Apr 2019 16:00:17 +0100 Subject: [PATCH] Fixed memory leak ... without breaking semantics of prior code. Possibly should change the semantics? For Peter / Antonin to comment --- Grid/qcd/smearing/StoutSmearing.h | 78 ++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 22 deletions(-) diff --git a/Grid/qcd/smearing/StoutSmearing.h b/Grid/qcd/smearing/StoutSmearing.h index 23a6b290..e4e11a63 100644 --- a/Grid/qcd/smearing/StoutSmearing.h +++ b/Grid/qcd/smearing/StoutSmearing.h @@ -1,5 +1,34 @@ +/************************************************************************************* + + Grid physics library, www.github.com/paboyle/Grid + + Source file: ./lib/qcd/smearing/StoutSmearing.h + + Copyright (C) 2019 + + Author: unknown + Author: Felix Erben + Author: Michael Marshall + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + See the full license in the file "LICENSE" in the top level distribution + directory + *************************************************************************************/ /* - @file stoutSmear.hpp + @file StoutSmearing.h @brief Declares Stout smearing class */ #ifndef STOUT_SMEAR_ @@ -12,37 +41,42 @@ namespace QCD { template class Smear_Stout : public Smear { private: - const Smear* SmearBase; - inline std::vector rho3D(double rho, int orthogdim){ + // Smear* ownership semantics: + // Smear* passed in to constructor are owned by caller, so we don't delete them here + // Smear* created within constructor need to be deleted as part of the destructor + const Smear* SmearBase; // Not owned by this object, so not deleted at destruction + const std::unique_ptr> OwnedBase; // deleted at destruction + +public: + INHERIT_GIMPL_TYPES(Gimpl) + + // only anticipated to be used from default constructor, but might as well be visible + inline static std::vector rho3D(double rho = 1.0, int orthogdim = -1){ std::vector rho3d(Nd*Nd); for (int mu=0; mu* base) : SmearBase(base) { - assert(Nc == 3);// "Stout smearing currently implemented only for Nc==3"); + /*! Stout smearing with base explicitly specified */ + Smear_Stout(Smear* base) : SmearBase{base} { + assert(Nc == 3 && "Stout smearing currently implemented only for Nc==3"); } - /*! Default constructor */ -/* Smear_Stout(double rho = 1.0) : SmearBase(new Smear_APE(rho)) { - assert(Nc == 3);// "Stout smearing currently implemented only for Nc==3"); - } */ + /*! Construct stout smearing object from explicitly specified rho matrix */ + Smear_Stout(const std::vector& rho_) + : OwnedBase{new Smear_APE(rho_)}, SmearBase{OwnedBase.get()} { + assert(Nc == 3 && "Stout smearing currently implemented only for Nc==3"); + } - /*! general constructor */ - Smear_Stout(std::vector& rho_) : SmearBase(new Smear_APE(rho_)) { - assert(Nc == 3 && "Stout smearing currently implemented only for Nc==3"); - } + /*! Default constructor. rho is constant in all directions, optionally except for orthogonal dimension */ + Smear_Stout(double rho = 1.0, int orthogdim = -1) + : OwnedBase{new Smear_APE(rho3D(rho,orthogdim))}, SmearBase{OwnedBase.get()} { + assert(Nc == 3 && "Stout smearing currently implemented only for Nc==3"); + } - /*! 3D constructor */ - Smear_Stout(double rho = 1.0, int orthogdim = -1) : SmearBase{new Smear_APE(rho3D(rho,orthogdim))} { - assert(Nc == 3 && "Stout smearing currently implemented only for Nc==3"); - } - - ~Smear_Stout() {delete SmearBase;} + ~Smear_Stout() {} // delete SmearBase... void smear(GaugeField& u_smr, const GaugeField& U) const { GaugeField C(U._grid);