c++ char* + std::vector memory leak : <p>The following code is reading a big object collection (95G of compressed objects that are uncompressed via the WriteObject streamer) from disk and prints their content as strings.</p>

<p>object.cxx:</p>

<pre><code>std::vector&lt;char&gt; ObjectHandler::GetObject(const std::string&amp; path)
{
  TFile *file = new TFile(path.c_str());

  // If file was not found or empty
  if (file-&gt;IsZombie()) {
    cout &lt;&lt; "The object was not found at " &lt;&lt; path &lt;&lt; endl;
  }

  // Get the AliCDBEntry from the root file
  AliCDBEntry *entry = (AliCDBEntry*)file-&gt;Get("AliCDBEntry");

  // Create an outcoming buffer
  TBufferFile *buffer = new TBufferFile(TBuffer::kWrite);

  // Stream and serialize the AliCDBEntry object to the buffer
  buffer-&gt;WriteObject((const TObject*)entry);

  // Obtain a pointer to the buffer
  char *pointer = buffer-&gt;Buffer();

  // Store the object to the referenced vector
  std::vector&lt;char&gt; vector(pointer, pointer + buffer-&gt;Length());

  // Release the open file
  delete file;

  delete buffer;

  return vector;
}
</code></pre>

<p>main.cxx:</p>

<pre><code>ObjectHandler objHandler;
boost::filesystem::path dataPath("/tmp");
boost::filesystem::recursive_directory_iterator endIterator;

if (boost::filesystem::exists(dataPath) &amp;&amp; boost::filesystem::is_directory(dataPath)) {
  for (static boost::filesystem::recursive_directory_iterator directoryIterator(dataPath); directoryIterator != endIterator;
     ++directoryIterator) {
    if (boost::filesystem::is_regular_file(directoryIterator-&gt;status())) {

      cout &lt;&lt; directoryIterator-&gt;path().string() &lt;&lt; endl;
      std::vector&lt;char&gt; vector = objHandler.GetObject(directoryIterator-&gt;path().string());
      cout &lt;&lt; vector &lt;&lt; endl;
      }
   }
}
</code></pre>

<p>1) Is calling by value the correct way to implement this method? Am i doing additional copies that could be avoided if calling by reference?</p>

<p>2) This code is leaking and i am suspecting that either the <em>char *pointer</em> is to blame, or the actual <em>std::vector</em> that is returned by the <em>ObjectHandler::GetObject()</em> method. I've tested the implementation with the following code:</p>

<pre><code>struct sysinfo sys_info;

sysinfo (&amp;sys_info);

cout &lt;&lt; "Total: " &lt;&lt; sys_info.totalram *(unsigned long long)sys_info.mem_unit / 1024 &lt;&lt; " Free: " &lt;&lt; sys_info.freeram *(unsigned long long)sys_info.mem_unit/ 1024 &lt;&lt; endl;
</code></pre>

<p>and the free ram is continuously reduced, until it reaches 0 and the program is killed. </p>
