Opened 5 years ago

Last modified 5 years ago

#13456 new Bugs

Object tracking fails with pointer to variant's content

Reported by: Ricardo Calheiros de Miranda Cosme <ricardo.cosme@…> Owned by: Robert Ramey
Milestone: To Be Determined Component: serialization
Version: Boost Development Trunk Severity: Problem
Keywords: variant tracking Cc:

Description

Object tracking fails with a pointer to an object contained into a variant that is an element of a container which loads the value_type into a local object. For example a map with a variant as a mapped_type.

There is a pull request: https://github.com/boostorg/serialization/pull/96

Demo:

struct Foo
{
    int i;

    template<typename Archive>
    void serialize(Archive& ar, const unsigned int version)
    { ar & i; }
};

int main()
{
    using map_t = std::unordered_map<std::string, boost::variant<Foo, float>>;
    std::string testfile{"/tmp/serialized"};
    {
        map_t map{{"key", Foo{5}}};
        auto ptr = &boost::strict_get<Foo>(map.begin()->second);
        
        std::ofstream os(testfile);
        boost::archive::text_oarchive oa(os);
        oa << map;
        oa << ptr;
    }
    {
        std::ifstream is(testfile, (std::ios_base::openmode)0);
        boost::archive::text_iarchive ia(is, 0);

        map_t map;
        Foo* ptr;
        
        ia >> map;
        ia >> ptr;
        
        assert(ptr->i == 5);
    }
}

Attachments (1)

bug_serialization_variant.tgz (743 bytes ) - added by Ricardo Calheiros de Miranda Cosme <ricardo.cosme@…> 5 years ago.
Demo

Download all attachments as: .zip

Change History (4)

by Ricardo Calheiros de Miranda Cosme <ricardo.cosme@…>, 5 years ago

Demo

comment:1 by Ricardo Calheiros de Miranda Cosme <ricardo.cosme@…>, 5 years ago

Version: Boost 1.63.0Boost Development Trunk

comment:2 by Ricardo Calheiros de Miranda Cosme <ricardo.cosme@…>, 5 years ago

Sorry, my pull request(#96) does not compile at some platforms. I will fix this.

comment:3 by Ricardo Calheiros de Miranda Cosme <ricardo.cosme@…>, 5 years ago

Pull request with a bugfix proposal https://github.com/boostorg/serialization/pull/98

Note: See TracTickets for help on using tickets.