'Valgrind report memory leak on google::protobuf::MessageLite::SerializeAsString() const

first my program was reported memory leak issue, and then I use valgrind to track it and get the below result:

==00:00:03:06.365 116370== 557,024 bytes in 1 blocks are definitely lost in loss record 192 of 192
==00:00:03:06.365 116370==    at 0x4C2E4AA: operator new(unsigned long) (vg_replace_malloc.c:344)
==00:00:03:06.365 116370==    by 0x79C1F48: allocate (new_allocator.h:111)
==00:00:03:06.365 116370==    by 0x79C1F48: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (basic_string.tcc:1057)
==00:00:03:06.365 116370==    by 0x79C2D4A: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned long) (basic_string.tcc:1078)
==00:00:03:06.365 116370==    by 0x79C2DF3: std::string::reserve(unsigned long) (basic_string.tcc:960)
==00:00:03:06.365 116370==    by 0x79C31E8: std::string::append(unsigned long, char) (basic_string.tcc:738)
==00:00:03:06.365 116370==    by 0x95E8E11: google::protobuf::STLStringResizeUninitialized(std::string*, unsigned long) (in /usr/lib64/libprotobuf.so.28.0.3)
==00:00:03:06.365 116370==    by 0x95E81A2: google::protobuf::MessageLite::AppendPartialToString(std::string*) const (in /usr/lib64/libprotobuf.so.28.0.3)
==00:00:03:06.365 116370==    by 0x95E805A: google::protobuf::MessageLite::AppendToString(std::string*) const (in /usr/lib64/libprotobuf.so.28.0.3)
==00:00:03:06.365 116370==    by 0x95E852B: google::protobuf::MessageLite::SerializeAsString() const (in /usr/lib64/libprotobuf.so.28.0.3)
==00:00:03:06.365 116370==    by 0x6E6F7E8: orc::WriterImpl::close() (c++/src/Writer.cc:445)
==00:00:03:06.365 116370==    by 0x6E66156: OrcWriter::close() (c++/src/OrcWrapper.cc:96)
==00:00:03:06.365 116370==    by 0x6E64E0E: close_orc_writer (c++/src/OrcWrapper.cc:627)

however this is really strange to me, here is a code snippet of orc::WriterImpl::close(), I can't see why it is possible to have leak.

{

  std::string fileFooter_str;
  ...
  std::unique_ptr<proto::Footer> tmp_footer(new proto::Footer());
  ...
  tmp_footer->MergeFrom(fileFooter);
  ...
  fileFooter_str = tmp_footer->SerializeAsString();
  ...
}

How can I have memory leak like the report above?

I try to reproduce the issue with below code, and run valgrind, nothing leak is reported.

#include "m1.pb.h"
#include <iostream>


int main(int argc, char *argv[])
{
    std::string s;
    Name name;
    while (true) {
        name.set_name("andy");
        s = name.SerializeAsString();
        std::cout << s.length() << "\n";
    }
    return 0;
}

cat m1.proto 
syntax = "proto3";

message Name {
    string name = 1 ; 
}

Anything wrong here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source