Troubles with a pesky (well understood) memory leak (protobuf/cpputest)

26 views
Skip to first unread message

MrMOSFET

unread,
Nov 12, 2024, 5:07:19 PM11/12/24
to cpputest
Hi all - I posted this in the github discussions as well, but it doesn't seem like that gets as much traffic. If I can solve this I will post the resolution there as well.

CppUTest 4.0
Protobuf 3.18.3
GCC 11.2.0

I am writing tests for a RPC client that is using protobuf under the hood - here is a very simple protofile to demonstrate the issue I am experiencing:

```protobuf
syntax = "proto3";
import "google/protobuf/any.proto";

package example.memleak;

message Payload {
  uint32 dummy = 1;
}

message Container {
  repeated google.protobuf.Any inner = 1;
}
```

And here is the test:
```cpp

static void fill_container(example::memleak::Container *container)
{
    example::memleak::Payload payload;
    payload.set_dummy(100);

    google::protobuf::Any *new_inner = container->add_inner();
    new_inner->PackFrom(payload);    
}

TEST(test_group, test_memleak)
{
    example::memleak::Container container;
    fill_container(&container);
    container.clear_inner();
}
```

This causes a leak because internally protobuf allocates pools of memory that it doesn't free until the program terminates.
I can fix this error by calling `google::protobuf::ShutdownProtobufLibrary();` but after this called all other tests using protobuf will segfault.

I tried adding `IGNORE_ALL_LEAKS_IN_TEST();` but this seems to have no effect.

I have also tried wrapping the test with `MemoryLeakWarningPlugin::saveAndDisableNewDeleteOverloads();` and `MemoryLeakWarningPlugin::restoreNewDeleteOverloads();` this causes a segfault - I haven't tracked down exactly why, but I suspect it is a mismatch between the version of new and delete used seeing protobuf might be freeing memory after the New/Delete overloads have been restored.

I am not sure if anyone has and suggestions or ideas on what to try next, but any advice would be appreciated.
Thanks.

Bas Vodde

unread,
Nov 24, 2024, 6:41:58 AM11/24/24
to CppUTest

Hi,

Sorry for late reply!


This is a common problem. The usual solutions are:

* See if the library you use has a method for clearing memory
* Try to allocate these in the main, so that it uses the buffer in the test
* You can disable the memory leak detection dynamically, although that can lead to problems….
* Turn off the memory leak overloads on compile time

Usually the first two ought to do it.

Thanks!

Bas


--
You received this message because you are subscribed to the Google Groups "cpputest" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cpputest+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/cpputest/a5a204a2-0bb5-4d12-9e0e-fa2f9065691dn%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages