0

I am running valgrind tool on our code base and I see many memory leaks in the below function.

std::string utils::toUtf8(const sdk::String& str)     
{
   ByteString byteStr = str.ToUtf8();
   return std::string(byteStr.GetConstData());    
}
  • sdk::String is our internal string object representation.
  • str.ToUtf8() returns a ByteString object which releases memory after its lifetime(at the end of this function).
  • getConstData() returns a const char pointer to the above ByteString object contents

Below is the back trace

==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<char> const&) (in /usr/lib/libstdc++.so.6.0.17)
==2833==    by 0x459BEE9D: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/libstdc++.so.6.0.17)
==2833==    by 0x459BF59A: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.17)
==2833==    by 0x1359C87B: utils::toUtf8(sdk::String const&) (String.cpp:17)

I am not able to understand why returning a simple string object is leaking memory.

Sirish
  • 9,183
  • 22
  • 72
  • 107
  • 1
    it probably means an objected was new'ed that contains a std::string – paulm Apr 22 '15 at 13:20
  • 1
    Actually no, as the `malloc` referred to by the stack trace is inside `std::string(char const *)`. Could you give us a minimal (non-)working example, Sirish ? EDIT: What I just said is actually wrong, if you `new` a `std::string *` and never `delete` it it might cause this error, as `std::string::~string` will never be called. – Ekleog Apr 22 '15 at 13:21
  • There is not enough code to reproduce the problem. Please post an [SSCCE](http://www.sscce.org). Also stating the exact version of your implementation of `std::string` would be helpful. – Baum mit Augen Apr 22 '15 at 13:23
  • We need to look the internal implementation of sdk::String. Without that information we can't give you a solution. – Jose Palma Apr 22 '15 at 13:25
  • Or http://stackoverflow.com/questions/1901322/valgrind-report-memory-leak-when-assign-a-value-to-a-string – Bill Apr 22 '15 at 13:30
  • 1
    @paulm : Yes this data string is returned to another object which is new'ed(http://projects.genivi.org/commonapi/documentation) but I have no idea about the lifecycle of the commonapi – Sirish Apr 22 '15 at 13:42
  • Your code sample looks fine. Show us how toUtf8 is called. – QuestionC Apr 22 '15 at 14:16

1 Answers1

1

Given the limited information you provide, the following is the best I can reach. According to the stack trace, the memory leaked is allocated by the temporary std::string object in std::string(byteStr.GetConstData()). So the problem has nothing to do with your ByteString. Since you are returning an r-value temporary by value, move construction happens. In case of a pre-C++11 compiler, copy elision happens. In either case, the allocated memory is not to be freed by the temporary within utils::toUtf8. But rather, the responsibility of freeing the memory is transferred to the variable receiving the value of utils::toUtf8(...). That is, the variable str in str = utils::toUtf8(...);, for example. To conclude, the problem is not in utils::toUtf8, but in other places.

Lingxi
  • 14,579
  • 2
  • 37
  • 93