Boost logo

Boost Users :

Subject: Re: [Boost-users] Serialization newbie, memory being overwritten?
From: Lasse Laursen (gazoo_at_[hidden])
Date: 2012-11-27 10:26:49


I'm using the xml archiving classes, which require the boost macro to be
used, otherwise compilation fails. Even if applying the changes you
recommended would work, that would mean that I can no longer store my
serialized data as xml.

But, I'll take and try most advice at this point and I can sadly report
that the result is the same using the text archiving classes from boost
and removing the macro as suggested.

As soon as I enter the serialize function declared in my class via the
>> operator (which I take it boost overloads), the remaining data in
aStream gets messed up for reasons that are still beyond me.

On 27-11-2012 16:27, Robert Ramey wrote:
> Lasse Laursen wrote:
>> Hello to you,
>>
>> I wonder if you might be able to solve this conundrum I've been
>> pondering. The boost (1.51) serialization library is giving me some
>> trouble. I've been battling back and forth with the library, and I'm
>> beginning to think I'm missing some key element.
>>
>> At first, I had issues to just getting things written out to a file
>> without crashes. As far as I could tell, this particular problem was
>> compounded by the fact, that I tried to simplify my code by just
>> writing a single primitive referenced by a pointer to an object
>> containing said primitive. As I've since come to learn, serializing
>> pointed-to primitives is apparently not trivial.
>>
>> I have since, gotten the serialization class to properly write out a
>> few simple member primitives from my class, but when I read them back
>> using the exact same function, it seems as though a lot - if not all
>> - of the pre-existing data in the object, which I am not serializing,
>> gets overwritten.
>>
>> As far as I have been able to tell from the tutorials, I am doing
>> everything exactly by the book. I was also concerned that maybe boost
>> doesn't allow partial serialization, but this shouldn't be an issue
>> either as far as I can tell. I must be making some sort of colossal
>> mistake that I just cannot see.
>>
>> My in and out code is fairly straight forward:
>>
>> std::string streamPath = PTMFilePath.string();
>> std::ifstream ifs(streamPath);
>> boost::archive::xml_iarchive ia(ifs);
>>
>> ia >> BOOST_SERIALIZATION_NVP(aStream);
>
> try
>
> ia >> aStream;
>
>>
>> -----
>>
>> std::string streamPath = PTMFilePath.string();
>> std::ofstream ofs(streamPath);
>> boost::archive::xml_oarchive oa(ofs);
>>
>> oa << BOOST_SERIALIZATION_NVP(aStream);
>
> oa << aStream;
>
>>
>> ------
>>
>> My serialization function:
>>
>> template<class Archive>
>> void serialize(Archive &ar, const unsigned int file_version)
>> {
>> ar & BOOST_SERIALIZATION_NVP(nrOfSamples);
>> }
>>
>> ------
>>
>> aStream is a pointer to a custom class, where I have allowed boost
>> access. I am really confused as to what is causing this. Both pointed
>> to data and regular primitives in the aStream class are wiped/altered
>> as soon as the program enteres the serialize function, and even
>> before the actual data seems to be read.
>>
>> I must be overlooking something really basic here. I hope someone can
>> whack me with the solution stick...
>>
>> Thanks,
>> Lasse


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