|
Boost Users : |
From: Robert S. Grimes (rsgrimes_at_[hidden])
Date: 2006-03-13 14:48:16
Oh, and note another thing. Putting a block around the read stuff doesn't
fix the problem. I had even originally moved it to another function. The
crucial thing is that the writing objects go out of scope - presumably it's
their destructors that clean things up?
> -----Original Message-----
> From: boost-users-bounces_at_[hidden]
> [mailto:boost-users-bounces_at_[hidden]]On Behalf Of Robert S.
> Grimes
> Sent: Monday, March 13, 2006 2:39 PM
> To: Boost-Users
> Subject: [Boost-users] [serialization] Problem writing and reading in
> sameblock, with work-around
>
>
> Hi all,
>
> It seems there is a problem with using both an output and an input archive
> in the same code block. Here is a simple sample that illustrates the
> problem. By defining the symbol BREAK_IT, you can see the problem; not
> defining it, the example works. What is particularly odd is that it is
> throwing an std::bad_alloc from within
> text_iarchive_impl<Archive>::load(std::string &s) in
> text_iarchive_impl.ipp
> because the requested size is obviously wrong - for example, 3435973837!!!
> I can't follow well enough what is happening, but I have figured out a
> work-around from noticing the differences between my example and demo.cpp;
> in the latter, the writing and reading of an archive are performed in two
> separate functions, or more precisely, two different blocks. Is this
> normal? Have I missed the documentation about this? Still, seems rather
> fussy, though to be honest, I'm not likely to suffer from the
> workaround in
> "production" code, but it's a bit unnerving nonetheless...
>
> Thanks!
> -Bob
>
> Example follows:
>
> #include <fstream>
> #include <iostream>
> #include <string>
>
> #include <boost/archive/text_iarchive.hpp>
> #include <boost/archive/text_oarchive.hpp>
>
> class Simple
> {
> private:
> int anIntMember;
> bool aBoolMember;
>
> template<class Archive>
> void serialize(Archive & ar, const unsigned int file_version)
> {
> ar & BOOST_SERIALIZATION_NVP(anIntMember)
> & BOOST_SERIALIZATION_NVP(aBoolMember);
> }
>
> // Grant access to serialization library
> friend class boost::serialization::access;
>
> public:
> Simple(): anIntMember(27), aBoolMember(false) {}
> int GetAnIntMember() const { return anIntMember; }
> void SetAnIntMember(int v) { anIntMember = v; }
> bool GetABoolMember() const { return aBoolMember; }
> void SetABoolMember(bool v) { aBoolMember = v; }
> };
>
> #define BREAK_IT
>
> void TestBoostText(const Simple& parms)
> {
> #ifndef BREAK_IT
> {
> #endif
> // Write out to boost archive
> std::ofstream ofs("./boost.txt");
> boost::archive::text_oarchive oa(ofs);
> oa << parms; /// Could use a better XML element name
> #ifndef BREAK_IT
> }
> #endif
>
> // Read it back and check
> Simple rparms;
> std::ifstream ifs("./boost.txt"); // THIS IS THE BROKEN LINE
> boost::archive::text_iarchive ia(ifs);
> ia >> rparms;
> std::cout << "Checking boost read...";
> if (rparms.GetAnIntMember() == 99) {
> std::cout << "Worked!!!" << std::endl;
> } else {
> std::cout << "Failed - appears not to have been updated" << std::endl;
> }
> }
>
> int main(int argc, char* argv[])
> {
> Simple parms;
>
> // Modify parms for testing purposes
> parms.SetAnIntMember(99);
>
> // Test both binary archives
> TestBoostText(parms);
>
> return 0;
> }
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
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