diff --git a/src/analysis/Histogram.cpp b/src/analysis/Histogram.cpp
index 0654a325517ae1a647173dd5f2b73ff9922b4fda..b645ec7a791502415fe91b7be58cd36a0686317e 100644
--- a/src/analysis/Histogram.cpp
+++ b/src/analysis/Histogram.cpp
@@ -401,7 +401,7 @@ void Histogram::prepareForAveraging() {
     std::vector<double> point( getNumberOfArguments() );
     for(unsigned i=0; i<point.size(); ++i) point[i]=getArgument(i);
     unsigned num_neigh; std::vector<unsigned> neighbors(1);
-    kernel.reset(myhist->getKernelAndNeighbors( point, num_neigh, neighbors ));
+    kernel=myhist->getKernelAndNeighbors( point, num_neigh, neighbors );
 
     if( num_neigh>1 ) {
       // Activate relevant tasks
diff --git a/src/gridtools/HistogramOnGrid.cpp b/src/gridtools/HistogramOnGrid.cpp
index 00a0a3b25cd2f1e49ada3e1389374549b0fad5e0..2644fd4af94e3ce2986d5f79a5b471984e0619c5 100644
--- a/src/gridtools/HistogramOnGrid.cpp
+++ b/src/gridtools/HistogramOnGrid.cpp
@@ -74,13 +74,13 @@ void HistogramOnGrid::setBounds( const std::vector<std::string>& smin, const std
   }
 }
 
-KernelFunctions* HistogramOnGrid::getKernelAndNeighbors( std::vector<double>& point, unsigned& num_neigh, std::vector<unsigned>& neighbors ) const {
+std::unique_ptr<KernelFunctions> HistogramOnGrid::getKernelAndNeighbors( std::vector<double>& point, unsigned& num_neigh, std::vector<unsigned>& neighbors ) const {
   if( discrete ) {
     plumed_assert( getType()=="flat" );
     num_neigh=1; for(unsigned i=0; i<dimension; ++i) point[i] += 0.5*dx[i];
     neighbors[0] = getIndex( point ); return NULL;
   } else if( getType()=="flat" ) {
-    KernelFunctions* kernel = new KernelFunctions( point, bandwidths, kerneltype, "DIAGONAL", 1.0 );
+    std::unique_ptr<KernelFunctions> kernel(new KernelFunctions( point, bandwidths, kerneltype, "DIAGONAL", 1.0 ));
     kernel->normalize( getVectorOfValues() ); getNeighbors( kernel->getCenter(), nneigh, num_neigh, neighbors );
     return kernel;
   } else if( getType()=="fibonacci" ) {
@@ -116,7 +116,7 @@ void HistogramOnGrid::calculate( const unsigned& current, MultiValue& myvals, st
     // Get the kernel
     unsigned num_neigh; std::vector<unsigned> neighbors;
     std::vector<double> der( dimension );
-    KernelFunctions* kernel=getKernelAndNeighbors( point, num_neigh, neighbors );
+    std::unique_ptr<KernelFunctions> kernel=getKernelAndNeighbors( point, num_neigh, neighbors );
 
     if( !kernel && getType()=="flat" ) {
       plumed_dbg_assert( num_neigh==1 ); der.resize(0);
@@ -169,7 +169,6 @@ void HistogramOnGrid::calculate( const unsigned& current, MultiValue& myvals, st
           buffer[ bufstart + gridbuf + nder + 1 + kder ] += myvals.getDerivative( 0, kder );
         }
       }
-      if( kernel ) delete kernel;
       for(unsigned i=0; i<dimension; ++i) delete vv[i];
     }
   }
diff --git a/src/gridtools/HistogramOnGrid.h b/src/gridtools/HistogramOnGrid.h
index 44d6988126977c049e531e50575e13d234afc98d..30af6d6332dd2c18dd62a86c759ce03803f9d3d2 100644
--- a/src/gridtools/HistogramOnGrid.h
+++ b/src/gridtools/HistogramOnGrid.h
@@ -23,6 +23,7 @@
 #define __PLUMED_gridtools_HistogramOnGrid_h
 
 #include "GridVessel.h"
+#include <memory>
 
 namespace PLMD {
 
@@ -51,7 +52,7 @@ public:
   virtual void accumulate( const unsigned& ipoint, const double& weight, const double& dens, const std::vector<double>& der, std::vector<double>& buffer ) const ;
   virtual void accumulateForce( const unsigned& ipoint, const double& weight, const std::vector<double>& der, std::vector<double>& intforce ) const ;
   unsigned getNumberOfBufferPoints() const ;
-  KernelFunctions* getKernelAndNeighbors( std::vector<double>& point, unsigned& num_neigh, std::vector<unsigned>& neighbors ) const;
+  std::unique_ptr<KernelFunctions> getKernelAndNeighbors( std::vector<double>& point, unsigned& num_neigh, std::vector<unsigned>& neighbors ) const;
   std::vector<Value*> getVectorOfValues() const ;
   void addOneKernelEachTimeOnly() { addOneKernelAtATime=true; }
   virtual void getFinalForces( const std::vector<double>& buffer, std::vector<double>& finalForces );