
Hi, I'm experiencing a runtime error while using boost's serialization for std::map. I am defining a map like: std::map<int, CSomeData> m; and serializing it as I do any other container or primitive type. However when the map contains more than two elements, there is a runtime exception when reading a serialized file back in. I'm using visual studio 8. Following is the exact source code I'm trying this with (very short sample app). Can anyone see what I'm doing wrong, or if there is an error with the boost code? I took a look at the written file in a binary editor and it looks fine. #include <boost/archive/binary_oarchive.hpp> #include <boost/archive/binary_iarchive.hpp> #include <boost/serialization/map.hpp> #include <fstream> #include <string> #include <map> using namespace std; // This is a test class to use as the map data. class CSomeData { public: CSomeData(){}; CSomeData(float f0, string str0) { m_f0 = f0; m_str0 = str0; } float m_f0; string m_str0; private: friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar, const unsigned int version) { ar & m_f0; ar & m_str0; } }; // This is the class we really want to try serializing. class CTest { public: CTest(){}; CTest(int nNumber) { m_nNumber = nNumber; // Fill with some dummy data. m_mTst.insert(make_pair(0, CSomeData(0.23f, "hi hi hi"))); m_mTst.insert(make_pair(1, CSomeData(7.65f, "second one"))); m_mTst.insert(make_pair(2, CSomeData(9.23f, "third one"))); m_mTst.insert(make_pair(3, CSomeData(5.6766, "chosen one"))); } ~CTest(){}; int m_nNumber; map<int, CSomeData> m_mTst; private: friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar, const unsigned int version) { ar & m_nNumber; ar & m_mTst; } }; int main() { std::ofstream ofs("filename"); // Write class instance to archive. Writing seems to work ok. const CTest ct(5); { boost::archive::binary_oarchive oa(ofs); oa << ct; } // Try to restore it sometime later. CTest ctNew; { std::ifstream ifs("filename", std::ios::binary); boost::archive::binary_iarchive ia(ifs); // READING CAUSES THE EXCEPTION! ia >> ctNew; } return 0; } Thanks, Mark

On 1/12/07, Mark Wyszomierski <markww@gmail.com> wrote:
Hi,
Hi Mark, with (very short sample app). Can anyone see what I'm doing wrong, or
if there is an error with the boost code?
No the boost code is OK. and in fact you are lucky that you get this error. If you look real closely to the "binary" output file you'll see that a byte that reads OA has been replaced by OD OA. Use std::ios::binary, or rather std::ios_base::binary, when you create the ofstream as well. hth, - levent -- Server Levent Yilmaz Mechanical Engineering @ PITT

Hi Levent, Thanks for the quick reply. I see what you mean. Anytime I open up a stream I now pass the ios_base::binary flag to the stream constructor. Still the same runtime error though. All I had to do to my example was the following, right?: std::ofstream ofs("filename", ios_base::binary); and std::ifstream ifs("filename", ios_base::binary); Thanks, Mark On 1/12/07, Server Levent Yilmaz <leventyilmaz@gmail.com> wrote:
On 1/12/07, Mark Wyszomierski <markww@gmail.com> wrote:
Hi,
Hi Mark,
with (very short sample app). Can anyone see what I'm doing wrong, or if there is an error with the boost code?
No the boost code is OK. and in fact you are lucky that you get this error. If you look real closely to the "binary" output file you'll see that a byte that reads OA has been replaced by OD OA.
Use std::ios::binary, or rather std::ios_base::binary, when you create the ofstream as well.
hth, - levent
-- Server Levent Yilmaz Mechanical Engineering @ PITT _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On 1/12/07, Mark Wyszomierski <markww@gmail.com> wrote:
Hi Levent,
Thanks for the quick reply. I see what you mean. Anytime I open up a stream I now pass the ios_base::binary flag to the stream constructor. Still the same runtime error though. All I had to do to my example was the following, right?:
std::ofstream ofs("filename", ios_base::binary);
yes, and with this fix your code works fine with my VC++ 7.1. (and without it it throws stream error as expected) One thing, do you get any compiler warnings related to RTTI? In 7.1 you need to turn it on (/GR switch) for serialization library to work. Then again, it should fail during output if this was the case. What exactly is the runtime error you're getting? Do you have any compilation warnings? -- Server Levent Yilmaz Mechanical Engineering @ PITT

Levent, I'm sorry, I edited my file incorrectly. It works perfectly after your update. Thanks for the help, Mark On 1/12/07, Server Levent Yilmaz <leventyilmaz@gmail.com> wrote:
On 1/12/07, Mark Wyszomierski <markww@gmail.com> wrote:
Hi Levent,
Thanks for the quick reply. I see what you mean. Anytime I open up a stream I now pass the ios_base::binary flag to the stream constructor. Still the same runtime error though. All I had to do to my example was the following, right?:
std::ofstream ofs("filename", ios_base::binary);
yes, and with this fix your code works fine with my VC++ 7.1. (and without it it throws stream error as expected)
One thing, do you get any compiler warnings related to RTTI? In 7.1 you need to turn it on (/GR switch) for serialization library to work. Then again, it should fail during output if this was the case.
What exactly is the runtime error you're getting? Do you have any compilation warnings?
--
Server Levent Yilmaz Mechanical Engineering @ PITT _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Mark Wyszomierski
-
Server Levent Yilmaz