Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2005-07-15 23:19:32


This is a different problem.

> DynamicArray<A> ArrayOfAs;
> ArrayOfAs.Element = new A[5];
> ArrayOfAs.Size = 5;
> A* secondElement = &ArrayOfAs.Element[1];
>
> std::stringstream stream;
> {
> boost::archive::text_oarchive oa(stream);
> oa & secondElement; // new object created here
> oa & ArrayOfAs; // attempt to reload an already loaded object
to new address - throw exception - pointer conflict
> }

the following should work:

       oa & ArrayOfAs; // load objects into array
       oa & secondElement; // load pointer to previouslt created object

Robert Ramey

Ivan Rachev wrote:
> Robert Ramey wrote:
>> Ahhh yes. Basically the STATIC_ASSERT is preventing you from setting
>> tracking for a primitive type. This almost HAS to be an error.
>> Doing this will effect serialization of all char's in your program
>> which is almost certainly what you don't want to do. If you want to
>> do this, You'll have to wrap a char in your own type so its not a
>> primitive anymore. Then it will pass.
>>
>
> Let's say the wrapper for char is A (from Martin's example). Then we
> would have a dynamic array of As. If I try to serialize a pointer
> pointing to one of these As inside the array, boost::serialization
> crashes. Example and stack trace follows at the end.
>
> Ivan
>
>
>
>
> #include <sstream>
>
> #include <boost/archive/text_iarchive.hpp>
> #include <boost/archive/text_oarchive.hpp>
> #include <boost/serialization/access.hpp>
> #include <boost/serialization/split_member.hpp>
>
> class A
> { friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive & ar, const unsigned int version) {}
> };
>
> template <typename T>
> struct DynamicArray
> {
> int Size;
> T* Element;
>
> friend class boost::serialization::access;
> template<class Archive>
> void save(Archive & ar, const unsigned int version) const
> {
> ar & Size;
> for (int i = 0; i < Size; ++i)
> ar & Element[i];
> }
> template<class Archive>
> void load(Archive & ar, const unsigned int version)
> {
> ar & Size;
> assert( Size >=0 );
> if (Size > 0)
> {
> Element = new T[Size];
> for (int i = 0; i < Size; ++i)
> ar & Element[i];
> }
> }
> BOOST_SERIALIZATION_SPLIT_MEMBER()
> };
>
> int main(int argc, char* argv[])
> {
> DynamicArray<A> ArrayOfAs;
> ArrayOfAs.Element = new A[5];
> ArrayOfAs.Size = 5;
> A* secondElement = &ArrayOfAs.Element[1];
>
> std::stringstream stream;
> {
> boost::archive::text_oarchive oa(stream);
> oa & secondElement;
> oa & ArrayOfAs;
> }
>
> delete [] ArrayOfAs.Element;
> }
>
>
>
>
> STACK:
> 77e9e8bb
> 77e9e8bb
> > _STL::__stl_debug_engine<bool>::_M_attach(_STL::__owned_list *
> __l=0x004f7db4, _STL::__owned_link * __c_node=0x00000001) Line 435 C++
> _STL::_Atomic_swap(volatile long * __p=0x00000001, long __q=1243908)
> Line 458 C++
> 004f7db4
> _CxxThrowException(void * pExceptionObject=0x0012fb14, const
> _s__ThrowInfo * pThrowInfo=0x004b9c84) C++
> boost::throw_exception<boost::archive::archive_exception>(const
> boost::archive::archive_exception & e={...}) Line 40 C++
>
>
boost::archive::detail::basic_oarchive_impl::save_object(boost::archive::det
ail::basic_oarchive
> & ar={...}, const void * t=0x004f5951, const
> boost::archive::detail::basic_oserializer & bos={...}) Line 284 C++
> boost::archive::detail::basic_oarchive::save_object(const void *
> x=0x004f5951, const boost::archive::detail::basic_oserializer &
> bos={...}) Line 395 C++
>
>
boost::archive::detail::save_non_pointer_type<boost::archive::text_oarchive,
A>::save::invokex(boost::archive::text_oarchive
> & ar={...}, const A & t={...}) Line 235 C++
>
>
boost::archive::detail::save_non_pointer_type<boost::archive::text_oarchive,
A>::invoke(boost::archive::text_oarchive
> & ar={...}, const A & t={...}) Line 309 C++
>
>
boost::archive::save<boost::archive::text_oarchive,A>(boost::archive::text_o
archive
> & ar={...}, const A & t={...}) Line 551 C++
>
>
boost::archive::basic_text_oarchive<boost::archive::text_oarchive>::save_ove
rride<A>()
> Line 71 C++
>
>
boost::archive::detail::interface_oarchive<boost::archive::text_oarchive>::o
perator<<<A>()
> Line 81 C++
>
>
boost::archive::detail::interface_oarchive<boost::archive::text_oarchive>::o
perator&<A>()
> Line 111 C++
> DynamicArray<A>::save<boost::archive::text_oarchive>() Line 26 C++
>
>
boost::serialization::access::member_save<boost::archive::text_oarchive,Dyna
micArray<A>
> >(boost::archive::text_oarchive & ar={...}, const DynamicArray<A> &
> t={...}, const unsigned int file_version=0) Line 91 C++
>
>
boost::serialization::detail::member_saver<boost::archive::text_oarchive,Dyn
amicArray<A>
> >::invoke(boost::archive::text_oarchive & ar={...}, const
> DynamicArray<A> & t={...}, const unsigned int file_version=0) Line 42
> C++
>
>
boost::serialization::split_member<boost::archive::text_oarchive,DynamicArra
y<A>
> >(boost::archive::text_oarchive & ar={...}, DynamicArray<A> &
> t={...}, const unsigned int file_version=0) Line 67 C++
> DynamicArray<A>::serialize<boost::archive::text_oarchive>() Line 40
> C++
>
>
boost::serialization::access::serialize<boost::archive::text_oarchive,Dynami
cArray<A>
> >(boost::archive::text_oarchive & ar={...}, DynamicArray<A> &
> t={...}, const unsigned int file_version=0) Line 107 C++
>
>
boost::serialization::serialize<boost::archive::text_oarchive,DynamicArray<A
>
> >(boost::archive::text_oarchive & ar={...}, DynamicArray<A> &
> t={...}, const unsigned int file_version=0) Line 78 C++
>
>
boost::serialization::serialize_adl<boost::archive::text_oarchive,DynamicArr
ay<A>
> >(boost::archive::text_oarchive & ar={...}, DynamicArray<A> &
> t={...}, const unsigned int file_version=0) Line 121 C++
>
>
boost::archive::detail::oserializer<boost::archive::text_oarchive,DynamicArr
ay<A>
> >::save_object_data(boost::archive::detail::basic_oarchive &
> ar={...}, const void * x=0x0012ff54) Line 152 C++
>
>
boost::archive::detail::basic_oarchive_impl::save_object(boost::archive::det
ail::basic_oarchive
> & ar={...}, const void * t=0x0012ff54, const
> boost::archive::detail::basic_oserializer & bos={...}) Line 257 C++
> boost::archive::detail::basic_oarchive::save_object(const void *
> x=0x0012ff54, const boost::archive::detail::basic_oserializer &
> bos={...}) Line 395 C++
>
>
boost::archive::detail::save_non_pointer_type<boost::archive::text_oarchive,
DynamicArray<A>
> >::save::invokex(boost::archive::text_oarchive & ar={...}, const
> DynamicArray<A> & t={...}) Line 235 C++
>
>
boost::archive::detail::save_non_pointer_type<boost::archive::text_oarchive,
DynamicArray<A>
> >::invoke(boost::archive::text_oarchive & ar={...}, const
> DynamicArray<A> & t={...}) Line 309 C++
> boost::archive::save<boost::archive::text_oarchive,DynamicArray<A>
> >(boost::archive::text_oarchive & ar={...}, const DynamicArray<A> &
> t={...}) Line 551 C++
>
>
boost::archive::basic_text_oarchive<boost::archive::text_oarchive>::save_ove
rride<DynamicArray<A>
> >() Line 71 C++
>
>
boost::archive::detail::interface_oarchive<boost::archive::text_oarchive>::o
perator<<<DynamicArray<A>
> >() Line 81 C++
>
>
boost::archive::detail::interface_oarchive<boost::archive::text_oarchive>::o
perator&<DynamicArray<A>
> >(DynamicArray<A> & t={...}) Line 111 C++
> main(int argc=1, char * * argv=0x004f7478) Line 55 C++
> mainCRTStartup() Line 398 C
> 77e9ca90


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net