Opened 12 years ago

Closed 12 years ago

#5106 closed Bugs (invalid)

std::bad_cast thrown in the boost::archive::basic_binary_(o|i)primitive constructor

Reported by: gleize.pierre@… Owned by: Robert Ramey
Milestone: To Be Determined Component: serialization
Version: Boost 1.45.0 Severity: Problem
Keywords: bad_cast serialization archive constructor Cc:

Description

Code highlighting:

   std::ofstream ofs( "./test.dat" );
   ::boost::archive::binary_oarchive oa( ofs );

Tested with gcc 4.6 on MacOSX, this simple piece of code throws a std::bad_cast exception with Boost 1.45.0

I found a quick fix.

Replace this :

include/boost/archive/basic_binary_oprimitive.hpp

108     BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY()) 
109     basic_binary_oprimitive(                                                                                                                                                               
110         std::basic_streambuf<Elem, Tr> & sb, 
111         bool no_codecvt
112     );

By this :

include/boost/archive/basic_binary_oprimitive.hpp

108     BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY()) 
109     basic_binary_oprimitive(                                                                                                                                                               
110         std::basic_streambuf<Elem, Tr> & sb, 
111         bool no_codecvt
112     ) : m_sb( sb )
113     #ifndef BOOST_NO_STD_LOCALE
114       , locale_saver( sb )
115     #endif  
116     {};

Same modifications for the file basic_binary_iprimitive.hpp.

I'm not sure this is the right thing to do, and I don't understand why the constructor didn't have any body. Is there a default behavior for that kind of constructor ?

Change History (2)

comment:1 by Robert Ramey, 12 years ago

Does making the following change in your test address the problem?

   std::ofstream ofs( "./test.dat", ios::binary ); // add ios::binary
   ::boost::archive::binary_oarchive oa( ofs );

Note you fix in the header would override the definition in the library. This could cause a number of unanticipated side effects. You might check the definition in the library to see how it compares to yours. Robert Ramey

comment:2 by Robert Ramey, 12 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.