I don't know is this is a bug as such, but thought I should bring it up.
I boiled down an un handled exception I was receiving in VC 7.1 and
related it to the demo.cpp
The demo does not close or flush the ofstream before it reads back from
the same file it wrote to, this appears to work correctly for the demo,
but in the attached very simple case it does not. I am assuming that it
is because the ofstream did not get flushed and the file contains no
data.
I think that adding a flush or close would make the demo more robust and
complete.
int _tmain(int argc, _TCHAR* argv[])
{
std::string filename(boost::archive::tmpdir());
filename += "/demofile.txt";
std::string b1("mike");
std::ofstream ofs(filename.c_str());
boost::archive::text_oarchive oa(ofs);
oa << b1;
// FIX
ofs.flush();
ofs.close(); // Without either of these VC7.1 gives
// Unhandled exception at 0x77e738b2 in
bstTest.exe: Microsoft C++ exception: //
boost::archive::archive_exception @ 0x0012f7bc. "
// END FIX
std::string b3;
std::ifstream ifs(filename.c_str());
boost::archive::text_iarchive ia(ifs);
ia >> b3;
}
Call stack ->
bstTest.exe!boost::throw_exceptionboost::archive::archive_exception(co
nst boost::archive::archive_exception & e={...}) Line 40 C++
bstTest.exe!boost::archive::detail::common_iarchive
Possible Serialization demo.cpp issue.Its my custom to use scoping for
automatic closing of streams and archives. With this, your example would
look more like:
int _tmain(int argc, _TCHAR* argv[])
{
std::string filename(boost::archive::tmpdir());
filename += "/demofile.txt";
std::string b1("mike");
{
std::ofstream ofs(filename.c_str());
boost::archive::text_oarchive oa(ofs);
oa << b1;
} // stream flushed and closed here. Then archive closed
}
This is the way its done in demo.cpp (by calling save/load in functions).
Robert Ramey
"Holmes, Michael A (Mike)"
participants (2)
-
Holmes, Michael A (Mike)
-
Robert Ramey