C++ memory leak when dynamically allocating DynamicMessageFactory

30 views
Skip to first unread message

Joseph Khell

unread,
Jun 26, 2024, 10:26:47 AM (7 days ago) Jun 26
to Protocol Buffers
Running the following code with address sanitizer:

    google::protobuf::DynamicMessageFactory* dynamic_message_factory = new google::protobuf::DynamicMessageFactory;
    delete dynamic_message_factory;

Causes the sanitizer to complain about new-delete type mismatch:

==26==ERROR: AddressSanitizer: new-delete-type-mismatch on 0xffff9661d9a0 in thread T0:
  object passed to delete has wrong type:
  size of the allocated type:   88 bytes;
  size of the deallocated type: 64 bytes.
    #0 0xffff9c8b6100 in operator delete(void*, unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:164
    #1 0xaaab09291a48 in google::protobuf::DynamicMessageFactory::~DynamicMessageFactory() (/project/test+0x202c1a48)

Samuel Benzaquen

unread,
Jun 28, 2024, 8:39:34 AM (5 days ago) Jun 28
to Joseph Khell, Protocol Buffers
Are you sure everything is compiled with the same options?
Turning on the ASan for parts of a binary is usually wrong, since it changes the ABI of the types and you will have mismatch.
You might have an ODR violation where part of your binary is built with ASan and another without.
From the difference in sizes it seems to me that the code that is calling `new DynamicMessageFactory` is built with ASan, but protobuf itself is built without.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/a2075a00-d547-4116-ab60-035e7cceefe5n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages