On Thu, Apr 3, 2025 at 4:31 AM Jiachen Liang <[email protected]>
wrote:

> 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?
>

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 [email protected].
> To view this discussion visit
> https://groups.google.com/d/msgid/protobuf/a0371b4a-4f94-4b4f-ae11-00655f36c38cn%40googlegroups.com
> <https://groups.google.com/d/msgid/protobuf/a0371b4a-4f94-4b4f-ae11-00655f36c38cn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/protobuf/CAM9aRs%2BDJ%2BU%3DxBYOBjvGbJxs%3D097-8mg%2BW%2BuJu7hwRKiGVWoww%40mail.gmail.com.

Reply via email to