Question

Why boost::iarchive constructor need correct strings buffer? i'm trying to init by empty stream, and later fill buffer and deserialization Data. but constructor error occured.

could think define archive when have correct buffer, but i need to defined by class member variable. because of several's register types by user.

#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <boost/archive/text_iarchive.hpp>

int main()
{
    std::istringstream iss ;
    boost::archive::text_iarchive iarchive(iss) ;
    return 0 ;
}

this code is error example

Was it helpful?

Solution

From looking briefly at the Boost source code, it appears that in the archive classes' constructors they try to read from the beginning of the stream to verify that it contains the correct content. For example, text archive data might begin with

22 serialization::archive

while xml archive data begins with

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="10">

This content is expected to be there and is read by the archive instance upon construction to verify the stream. Data that was written by an application (and that your application would want to read) would follow this header data.

This would explain why your application is failing when you construct the text_iarchive instance with an empty stream.

One solution I can suggest to solve your problem is to delay the creation of your archive object until a valid data stream is available. If the archive object has to be a class member, the simplest way to do that is to declare it as a pointer type (perhaps std::unique_ptr<boost::archive::text_iarchive>) so that it can be null initially until you need it (otherwise create it on the stack within a function and call it when needed).

OTHER TIPS

problem was solved. archive have to class member was because of regist several types. so make pointer is too not solution. so i wander a long time, and i found there is macro to register type, so not have to access archive object. so i use that, and i solve problem.

thanks to answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top