Skip to content

Track variant selectively so reused values round-trip - #360

Open
gennaroprota wants to merge 1 commit into
developfrom
fix/variant-value-tracking-drops-repeated-values
Open

Track variant selectively so reused values round-trip#360
gennaroprota wants to merge 1 commit into
developfrom
fix/variant-value-tracking-drops-repeated-values

Conversation

@gennaroprota

@gennaroprota gennaroprota commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

boost::variant and std::variant specialized tracking_level to track_always. With object tracking on, serializing the same variant twice by value wrote the second occurrence as a bare object reference. On load, that reference could not be resolved into a separate destination: value loads receive their destination by value, not by reference, so the tracking table has nowhere to copy from, and the second variant silently loaded as empty.

So, use track_selectively (the default for class types) for all three variant flavors: a variant is tracked when serialized through a pointer and stored by value otherwise, matching every other value type. The tracking flag is written per object in the class info, so archives written with the old track_always variant still load correctly and no archive version bump is needed.

Also add a regression test that serializes a variant twice into one archive and checks that both copies load back to the original value.

Closes #203.

`boost::variant` and `std::variant` specialized `tracking_level` to
`track_always`. With object tracking on, serializing the same variant
twice by value wrote the second occurrence as a bare object reference.
On load, that reference could not be resolved into a separate
destination: value loads receive their destination by value, not by
reference, so the tracking table has nowhere to copy from, and the
second variant silently loaded as empty.

So, use `track_selectively` (the default for class types) for all three
variant flavors: a variant is tracked when serialized through a pointer
and stored by value otherwise, matching every other value type. The
tracking flag is written per object in the class info, so archives
written with the old track_always variant still load correctly and no
archive version bump is needed.

Also add a regression test that serializes a variant twice into one
archive and checks that both copies load back to the original value.

Closes #203.
@gennaroprota
gennaroprota force-pushed the fix/variant-value-tracking-drops-repeated-values branch from f00098c to 45f9f03 Compare July 29, 2026 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

boost::variant not correctly serialized when reusing the memory location

1 participant