title: valgrind and std::unique_ptr --- false positive or not?
body: <p>I am trying to understand the use of smart pointers in modern C++, and I have written a small, simple program to test in <code>valgrind</code>. The problem is that the example below:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;memory&gt;
  
  class Base {
      private:
          virtual double meth_1( double x ) const = 0;
          virtual void meth_2( int y ) const = 0;
      protected:
          Base()
          {
              std::cout &lt;&lt; "ctor of base for: " &lt;&lt; this &lt;&lt; std::endl;
          }
      public:
          virtual ~Base()
          {
              std::cout &lt;&lt; "dtor of base for: " &lt;&lt; this &lt;&lt; std::endl;
          }
          double IMeth_1( double x ) const
          {
              return meth_1(x);
          }
          void IMeth_2( int y ) const
          {
              meth_2(y);
          }
  };
  
  class Derived_1 : public Base {
      private:
          double meth_1( double x ) const final
          {
              return x + 5.0;
          }
          void meth_2( int y ) const final
          {
              std::cout &lt;&lt; (y + 5) &lt;&lt; std::endl;
          }
      public:
          Derived_1() : Base()
          {
              std::cout &lt;&lt; "ctor of Derived_1: " &lt;&lt; this &lt;&lt; std::endl;
          }
          ~Derived_1()
          {
              std::cout &lt;&lt; "dtor of Derived_1: " &lt;&lt; this &lt;&lt; std::endl;
          }
  };
  
  class Derived_2 : public Base {
      private:
          double meth_1( double x ) const final
          {
              return x + 10.0;
          }
          void meth_2( int y ) const final
          {
              std::cout &lt;&lt; (y + 10) &lt;&lt; std::endl;
          }
      public:
          Derived_2() : Base()
          {
              std::cout &lt;&lt; "ctor of Derived_2: " &lt;&lt; this &lt;&lt; std::endl;
          }
          ~Derived_2()
          {
              std::cout &lt;&lt; "dtor of Derived_2: " &lt;&lt; this &lt;&lt; std::endl;
          }
  };
  
  void Fun( const Base&amp; crBase )
  {
      crBase.IMeth_2( 5 );
  }
  
  int main( int argc, char* argv[] ) {
      std::unique_ptr&lt; Base &gt; upBase;
  
      for ( std::size_t idx = 0ul; idx &lt; 2ul; idx++ ) {
          upBase      = std::make_unique&lt; Derived_1 &gt;();
          std::cout   &lt;&lt; upBase-&gt;IMeth_1( idx )   &lt;&lt; std::endl;
          upBase-&gt;IMeth_2( idx );
          std::cout   &lt;&lt; "----------"             &lt;&lt; std::endl;
      }
  
      for ( std::size_t idx = 0ul; idx &lt; 2ul; idx++ ) {
          upBase      = std::make_unique&lt; Derived_2 &gt;();
          std::cout   &lt;&lt; upBase-&gt;IMeth_1( idx )   &lt;&lt; std::endl;
          upBase-&gt;IMeth_2( idx );
          std::cout   &lt;&lt; "----------"             &lt;&lt; std::endl;
      }
  
      upBase = std::make_unique&lt; Derived_1 &gt;();
      Fun( *upBase );
  
      return 0;
  }
  </code></pre>
  
  <p>gives a memory leak when run with <code>valgrind --leak-check=full --show-leak-kinds=all &lt;prog_name&gt;</code>:</p>
  
  <pre><code>==32350== HEAP SUMMARY:
  ==32350==     in use at exit: 72,704 bytes in 1 blocks
  ==32350==   total heap usage: 6 allocs, 5 frees, 72,744 bytes allocated
  ==32350== 
  ==32350== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
  ==32350==    at 0x4C28C10: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==32350==    by 0x4EBE1EF: pool (eh_alloc.cc:117)
  ==32350==    by 0x4EBE1EF: __static_initialization_and_destruction_0 (eh_alloc.cc:244)
  ==32350==    by 0x4EBE1EF: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:307)
  ==32350==    by 0x400F279: call_init.part.0 (in /usr/lib/ld-2.22.so)
  ==32350==    by 0x400F38A: _dl_init (in /usr/lib/ld-2.22.so)
  ==32350==    by 0x4000DB9: ??? (in /usr/lib/ld-2.22.so)
  ==32350== 
  ==32350== LEAK SUMMARY:
  ==32350==    definitely lost: 0 bytes in 0 blocks
  ==32350==    indirectly lost: 0 bytes in 0 blocks
  ==32350==      possibly lost: 0 bytes in 0 blocks
  ==32350==    still reachable: 72,704 bytes in 1 blocks
  ==32350==         suppressed: 0 bytes in 0 blocks
  ==32350== 
  ==32350== For counts of detected and suppressed errors, rerun with: -v
  ==32350== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
  </code></pre>
  
  <p>Is that block with 72,704 bytes still in use at exit a false positive, or am I misusing the smart pointer? I assume I am not doing any kind of slicing as the base dtor is called each time the object is deleted.</p>
  
  <p>Sorry if this has been a dumb question, but I could not find any valgrind/false positive/unique_ptr-related topic in SO. Moreover, I am not aware of any additional block that might have been created in <code>unique_ptr</code> similar to that in <code>shared_ptr</code> to keep track of the object.</p>
  
  <p><strong>Edit:</strong> Not a duplicate of <a href="https://stackoverflow.com/questions/3840582">Still Reachable Leak detected by Valgrind</a>, as in my case I am not using threads (and <code>valgrind</code> is known to give false positives in, especially, OpenMPI environments). Moreover, in the other question, the problem is solved by a proper modification to the code provided. Still, there is not any consensus on what to call a <em>true memory leak</em> --- that is, should the <em>reachable blocks</em> that are <em>still in use at exit</em> be considered as <em>memory leaks</em>, or not.</p>