Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2008-01-18 12:43:39


Boris Gubenko wrote:
> The situation is that with polymorphic base class, serialization
> library throws an exception. With non-polymorphic base class, the
> program succeeds.

Actually it fails in all cases.

If the base is polymorphic, its failing because the derived class isn't
registered or exported. This is dectected at runtime and an exception
is thrown.

If the base isn't polymorphic, then the instance is "sliced" and ONLY
the base class information is saved. Your example doesn't have
any data so this is not detected in this case. That is - it seems to
work but will fail if you add data to the derived class.

Robert Ramey

>
> You are, probably, right that while x.cpp in my previous mail and
> test_exported.cpp throw the same exception, the cause may be
> different. I tried x.cpp (with polymorphic base class) with aCC
> instead of gcc and got:
>
> aCC runtime: Uncaught exception of type
> "boost::archive::archive_exception".
>
> At the same time, test_exported succeeds with aCC.
>
> I'll try serialization library at rrsd.com when I have a chance.
> In the meantime, do you have any idea why the program might
> be throwing exception with 1.35?
>
> Thanks,
> Boris
>
> Robert Ramey wrote:
>> One can't invoke any function of the derived class through a base
>> class pointer unless the base class is polymorphic - that is has at
>> least one virtual function.
>>
>> Serialization is no different in this regard.
>>
>> I would not expect the behavior of the program below to
>> change between version.
>>
>> The error message is the same as those failures in test_export
>> but the cause isn't necessarily the same.
>>
>> I would be curious of the version 1.36 serialization library
>> at rrsd.com would pass tests with gcc 4.1 and higher.
>>
>> Robert Ramey
>>
>> Boris Gubenko wrote:
>>> x.cpp below reproduces failures of test_exported* tests on
>>> several platforms using gcc 4.1 and higher. The tests fail with:
>>>
>>> terminate called after throwing an instance of
>>> 'boost::archive::archive_exception'
>>> what(): unregistered void cast
>>>
>>> When compiled with macro WORKS defined, i.e. when destructor of
>>> the base class is removed, reproducer succeeds. Does it give any
>>> clue?
>>>
>>> Thanks,
>>> Boris
>>>
>>> x.cpp
>>> -----
>>> #include <fstream>
>>> #include <boost/archive/binary_oarchive.hpp>
>>>
>>> struct polymorphic_base
>>> {
>>> template<class Archive>
>>> void serialize(Archive&, const unsigned int) {}
>>> #ifndef WORKS
>>> virtual ~polymorphic_base(){}
>>> #endif
>>> };
>>>
>>> struct polymorphic_derived : polymorphic_base
>>> {
>>> template<class Archive>
>>> void serialize(Archive&, const unsigned int) {}
>>> };
>>>
>>> int main()
>>> {
>>> std::ofstream os("x.x");
>>> boost::archive::binary_oarchive oa(os);
>>> polymorphic_base *rb = new polymorphic_derived;
>>> oa << BOOST_SERIALIZATION_NVP(rb);
>>> }
>>>
>>>
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk