Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

CheckTypeAndMergeFrom disturbs cross-binary usage

91 views
Skip to first unread message

Jiachen Liang

unread,
Apr 3, 2025, 4:31:04 AMApr 3
to Protocol Buffers
Consider this case: 
static lib A ->  this contains my protobuf definition of MyMessage
dll B -> links A
exe C -> links A

In run time: a protobuf message was passing from dll B to exe C, when exe C trying to store it locally by assign the message in a local variable, CheckTypeAndMergeFrom failed and triggered fatal error. The warning looks like this:

"Invalid call to CheckTypeAndMergeFrom between types MyMessage and MyMessage“

It seems the type check compares the pointer of ClassData, in my case there two instance so the comparison failed.

A, B and C are all from one same code base and always compiled together, so the message definition won't diverge. The type check has limitation in this case, is there any way to alleviate this?


Samuel Benzaquen

unread,
Apr 3, 2025, 10:58:07 AMApr 3
to Jiachen Liang, Protocol Buffers
You have an ODR violation in your program. You can see details for ODR here: https://en.cppreference.com/w/cpp/language/definition
Note that an ODR violation means the program is ill-formed and there are no guarantees by the language specification.

You have two copies of the same thing in your binary and that breaks the rules of C++.
Generally the way to solve this is changing how the binary/libraries are linked to make sure there is only one of each translation unit in the final build.

A less optimal solution when ODR is violated is to never pass messages or any runtime type between the modules. Instead, you could pass serialized bytes, type names, etc between the modules.
 


--
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 visit https://groups.google.com/d/msgid/protobuf/a0371b4a-4f94-4b4f-ae11-00655f36c38cn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages