Skip to content

(C++) memory leak in PackFrom() on arena-allocated Any objects #8264

Closed
@besteinm

Description

@besteinm

What version of protobuf and what language are you using?
Version: 3.14.0, 3.15.8
Language: C++
The protobuf library has been compiled with the following command line:

cmake -G "Visual Studio 15 2017 Win64" -Dprotobuf_BUILD_SHARED_LIBS=ON ..

What operating system (Linux, Windows, ...) and version?
Windows 10 64bit

What runtime / compiler are you using (e.g., python version or gcc version)
Microsoft Visual Studio Professional 2017 15.9.24

What did you do?
With the following proto definition (my_message.proto):

syntax = "proto3";

message MyMessage {
}

and the following test program:

#include "my_message.pb.h"
#include <google/protobuf/arena.h>
#include <google/protobuf/message.h>
#include <google/protobuf/any.pb.h>
#include <iostream>

std::uint64_t leak()
{
  google::protobuf::Arena arena;

  google::protobuf::Any* any_msg = google::protobuf::Arena::CreateMessage<google::protobuf::Any>(&arena);
  MyMessage my_msg;

  any_msg->PackFrom(my_msg);    //  <--  this causes the memory leak

  return arena.SpaceUsed();   // [1]
}

int main()
{
  std::uint64_t max_size = 0;
  std::uint16_t n = 0;

  while (true) {
    const std::uint64_t size = leak();

    max_size = std::max(size, max_size);
    ++n;
               
    if (n == 0) {
      std::cout << "max = " << max_size << std::endl;
    }
  }

  return 0;
}

Memory usage of the program grows indefinitely (observed with Process Explorer and Visual Studio diagnostic tool). When however the Any object in leak() is allocated without an arena (i.e just with plain google::protobuf::Any any_msg;) the memory usage stays constant.
It makes no difference whether the MyMessage object "my_msg" is arena-allocated or not.

[1] arena.SpaceUsed() always returns the same value, even when the memory usage shown in the diagnostic tool is growing.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions