title: Valgrind memory leak when returning std::string
body: <p>I am running valgrind tool on our code base and I see many memory leaks in the below function. </p>
  
  <pre><code>std::string utils::toUtf8(const sdk::String&amp; str)     
  {
     ByteString byteStr = str.ToUtf8();
     return std::string(byteStr.GetConstData());    
  }
  </code></pre>
  
  <ul>
  <li>sdk::String is our internal string object representation. </li>
  <li>str.ToUtf8() returns a ByteString  object which releases memory after its lifetime(at the end of this function). </li>
  <li>getConstData() returns a const char pointer to the above ByteString object contents</li>
  </ul>
  
  <p>Below is the back trace</p>
  
  <pre><code>==2833== 3,830 bytes in 160 blocks are definitely lost in loss record 33,600 of 34,381
  ==2833==    at 0x40084DA: malloc (vg_replace_malloc.c:296)
  ==2833==    by 0x43898CC: SCHeapAlloc (in /opt/sw/libSystemCommon-2.7.so)
  ==2833==    by 0x459BCF64: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/libstdc++.so.6.0.17)
  ==2833==    by 0x459BEE9D: char* std::string::_S_construct&lt;char const*&gt;(char const*, char const*, std::allocator&lt;char&gt; const&amp;, std::forward_iterator_tag) (in /usr/lib/libstdc++.so.6.0.17)
  ==2833==    by 0x459BF59A: std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char const*, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/libstdc++.so.6.0.17)
  ==2833==    by 0x1359C87B: utils::toUtf8(sdk::String const&amp;) (String.cpp:17)
  </code></pre>
  
  <p>I am not able to understand why returning a simple string object is leaking memory. </p>