使用boost boost :: archive :: text_oarchive.hpp对反序列化字符串进行Segault boost :: archive :: archive_exception

时间:2016-02-24 13:07:59

标签: c++ serialization boost

我第一次尝试使用boost库进行序列化,并遇到了反序列化字符串的问题。我一直关注这里的例子http://www.boost.org/doc/libs/1_60_0/libs/serialization/doc/tutorial.html并成功运行提供的demo.cpp。

然而,当我转到我自己的程序时,序列化然后尝试反序列化我得到了一个segault。错误消息是:

terminate called after throwing an instance of 'boost::archive::archive_exception'
  what():  input stream error

gdb提供以下堆栈跟踪:

#0  0x00000000004b50e7 in raise ()
#1  0x00000000004b52da in abort ()
#2  0x00000000004959cd in __gnu_cxx::__verbose_terminate_handler() ()
#3  0x0000000000495706 in __cxxabiv1::__terminate(void (*)()) ()
#4  0x0000000000495751 in std::terminate() ()
#5  0x0000000000415619 in __cxa_throw ()
#6  0x0000000000406531 in boost::serialization::throw_exception<boost::archive::archive_exception> (
    e=...) at /usr/local/include/boost/serialization/throw_exception.hpp:36
#7  0x000000000040bf87 in boost::archive::text_iarchive_impl<boost::archive::text_iarchive>::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) ()
#8  0x00000000004040c9 in boost::archive::load_access::load_primitive<boost::archive::text_iarchive, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > (ar=..., t=...)
    at /usr/local/include/boost/archive/detail/iserializer.hpp:107
#9  0x000000000040406e in boost::archive::detail::load_non_pointer_type<boost::archive::text_iarchive>::load_primitive::invoke<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > (ar=..., t=...) at /usr/local/include/boost/archive/detail/iserializer.hpp:362
#10 0x0000000000404015 in boost::archive::detail::load_non_pointer_type<boost::archive::text_iarchive>::invoke<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > (
    ar=..., t=...) at /usr/local/include/boost/archive/detail/iserializer.hpp:439
#11 0x0000000000403fb2 in boost::archive::load<boost::archive::text_iarchive, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > (ar=..., t=...)
    at /usr/local/include/boost/archive/detail/iserializer.hpp:592
#12 0x0000000000403f5b in boost::archive::detail::common_iarchive<boost::archive::text_iarchive>::load_override<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > (
    this=0x7fffffffdb10, t=...) at /usr/local/include/boost/archive/detail/common_iarchive.hpp:66
#13 0x0000000000403efb in boost::archive::basic_text_iarchive<boost::archive::text_iarchive>::load_override<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > (
    this=0x7fffffffdb10, t=...) at /usr/local/include/boost/archive/basic_text_iarchive.hpp:65
---Type <return> to continue, or q <return> to quit---
#14 0x0000000000403ea1 in boost::archive::text_iarchive_impl<boost::archive::text_iarchive>::load_override<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > (
    this=0x7fffffffdb10, t=...) at /usr/local/include/boost/archive/text_iarchive.hpp:82
#15 0x0000000000403e09 in boost::archive::detail::interface_iarchive<boost::archive::text_iarchive>::operator>><std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > (
    this=0x7fffffffdb10, t=...) at /usr/local/include/boost/archive/detail/interface_iarchive.hpp:60
#16 0x0000000000403d3c in boost::archive::detail::interface_iarchive<boost::archive::text_iarchive>::operator&<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > (
    this=0x7fffffffdb10, t=...) at /usr/local/include/boost/archive/detail/interface_iarchive.hpp:67
#17 0x0000000000403c8f in Character::serialize<boost::archive::text_iarchive> (this=0x7e0260, 
    ar=..., version=5) at Character.h:47
#18 0x0000000000403bf1 in boost::serialization::access::serialize<boost::archive::text_iarchive, Character> (ar=..., t=..., file_version=5) at /usr/local/include/boost/serialization/access.hpp:118
#19 0x0000000000403b66 in boost::serialization::serialize<boost::archive::text_iarchive, Character>
    (ar=..., t=..., file_version=5) at /usr/local/include/boost/serialization/serialization.hpp:69
#20 0x0000000000403989 in boost::serialization::serialize_adl<boost::archive::text_iarchive, Character> (ar=..., t=..., file_version=5) at /usr/local/include/boost/serialization/serialization.hpp:128
#21 0x000000000040376c in boost::archive::detail::iserializer<boost::archive::text_iarchive, Character>::load_object_data (
    this=0x7beed0 <boost::serialization::singleton<boost::archive::detail::iserializer<boost::archive::text_iarchive, Character> >::get_instance()::t>, ar=..., x=0x7e0260, file_version=5)
    at /usr/local/include/boost/archive/detail/iserializer.hpp:188
#22 0x0000000000406fd8 in boost::archive::detail::basic_iarchive::load_object(void*, boost::archive::detail::basic_iserializer const&) ()
#23 0x0000000000404245 in boost::archive::detail::load_non_pointer_type<boost::archive::text_iarchive>::load_standard::invoke<Character> (ar=..., t=...)
    at /usr/local/include/boost/archive/detail/iserializer.hpp:387
#24 0x000000000040420f in boost::archive::detail::load_non_pointer_type<boost::archive::text_iarchive>::invoke<Character> (ar=..., t=...) at /usr/local/include/boost/archive/detail/iserializer.hpp:439
#25 0x00000000004041d8 in boost::archive::load<boost::archive::text_iarchive, Character> (ar=..., 
    t=...) at /usr/local/include/boost/archive/detail/iserializer.hpp:592
#26 0x00000000004041ad in boost::archive::detail::common_iarchive<boost::archive::text_iarchive>::load_override<Character> (this=0x7fffffffdb10, t=...)
    at /usr/local/include/boost/archive/detail/common_iarchive.hpp:66
#27 0x0000000000404179 in boost::archive::basic_text_iarchive<boost::archive::text_iarchive>::load_override<Character> (this=0x7fffffffdb10, t=...)
    at /usr/local/include/boost/archive/basic_text_iarchive.hpp:65
#28 0x000000000040414b in boost::archive::text_iarchive_impl<boost::archive::text_iarchive>::load_override<Character> (this=0x7fffffffdb10, t=...)
    at /usr/local/include/boost/archive/text_iarchive.hpp:82
---Type <return> to continue, or q <return> to quit---
#29 0x0000000000404111 in boost::archive::detail::interface_iarchive<boost::archive::text_iarchive>::operator>><Character> (this=0x7fffffffdb10, t=...)
    at /usr/local/include/boost/archive/detail/interface_iarchive.hpp:60
#30 0x00000000004040a3 in boost::serialization::nvp<Character>::load<boost::archive::text_iarchive>
    (this=0x7fffffffd7e0, ar=...) at /usr/local/include/boost/serialization/nvp.hpp:87
#31 0x0000000000404041 in boost::serialization::access::member_load<boost::archive::text_iarchive, boost::serialization::nvp<Character> > (ar=..., t=..., file_version=0)
    at /usr/local/include/boost/serialization/access.hpp:101
#32 0x0000000000403fde in boost::serialization::detail::member_loader<boost::archive::text_iarchive, boost::serialization::nvp<Character> >::invoke (ar=..., t=..., file_version=0)
    at /usr/local/include/boost/serialization/split_member.hpp:54
#33 0x0000000000403f87 in boost::serialization::split_member<boost::archive::text_iarchive, boost::serialization::nvp<Character> > (ar=..., t=..., file_version=0)
    at /usr/local/include/boost/serialization/split_member.hpp:69
#34 0x0000000000403f27 in boost::serialization::nvp<Character>::serialize<boost::archive::text_iarchive> (this=0x7fffffffd7e0, ar=..., file_version=0)
    at /usr/local/include/boost/serialization/nvp.hpp:89
#35 0x0000000000403ecd in boost::serialization::access::serialize<boost::archive::text_iarchive, boost::serialization::nvp<Character> > (ar=..., t=..., file_version=0)
    at /usr/local/include/boost/serialization/access.hpp:118
#36 0x0000000000403e40 in boost::serialization::serialize<boost::archive::text_iarchive, boost::serialization::nvp<Character> > (ar=..., t=..., file_version=0)
    at /usr/local/include/boost/serialization/serialization.hpp:69
#37 0x0000000000403d92 in boost::serialization::serialize_adl<boost::archive::text_iarchive, boost::serialization::nvp<Character> > (ar=..., t=..., file_version=0)
    at /usr/local/include/boost/serialization/serialization.hpp:128
#38 0x0000000000403cdf in boost::archive::detail::load_non_pointer_type<boost::archive::text_iarchive>::load_only::invoke<boost::serialization::nvp<Character> > (ar=..., t=...)
    at /usr/local/include/boost/archive/detail/iserializer.hpp:373
#39 0x0000000000403c28 in boost::archive::detail::load_non_pointer_type<boost::archive::text_iarchive>::invoke<boost::serialization::nvp<Character> const> (ar=..., t=...)
    at /usr/local/include/boost/archive/detail/iserializer.hpp:439
#40 0x0000000000403b91 in boost::archive::load<boost::archive::text_iarchive, boost::serialization::nvp<Character> const> (ar=..., t=...) at /usr/local/include/boost/archive/detail/iserializer.hpp:592
#41 0x0000000000403a05 in boost::archive::detail::common_iarchive<boost::archive::text_iarchive>::load_override<boost::serialization::nvp<Character> const> (this=0x7fffffffdb10, t=...)
    at /usr/local/include/boost/archive/detail/common_iarchive.hpp:66
#42 0x000000000040380d in boost::archive::basic_text_iarchive<boost::archive::text_iarchive>::load_override<boost::serialization::nvp<Character> const> (this=0x7fffffffdb10, t=...)
    at /usr/local/include/boost/archive/basic_text_iarchive.hpp:65
---Type <return> to continue, or q <return> to quit---
#43 0x00000000004036c7 in boost::archive::text_iarchive_impl<boost::archive::text_iarchive>::load_override<boost::serialization::nvp<Character> const> (this=0x7fffffffdb10, t=...)
    at /usr/local/include/boost/archive/text_iarchive.hpp:82
#44 0x000000000040330d in boost::archive::detail::interface_iarchive<boost::archive::text_iarchive>::operator>><boost::serialization::nvp<Character> const> (this=0x7fffffffdb10, t=...)
    at /usr/local/include/boost/archive/detail/interface_iarchive.hpp:60
#45 0x0000000000402de2 in boost::archive::detail::pointer_iserializer<boost::archive::text_iarchive, Character>::load_object_ptr (
    this=0x7beeb0 <boost::serialization::singleton<boost::archive::detail::pointer_iserializer<boost::archive::text_iarchive, Character> >::get_instance()::t>, ar=..., x=@0x7fffffffdb08: 0x7e0260, 
    file_version=5) at /usr/local/include/boost/archive/detail/iserializer.hpp:331
#46 0x00000000004078bf in boost::archive::detail::basic_iarchive_impl::load_pointer(boost::archive::detail::basic_iarchive&, void*&, boost::archive::detail::basic_pointer_iserializer const*, boost::archive::detail::basic_pointer_iserializer const* (*)(boost::serialization::extended_type_info const&))
    ()
#47 0x00000000004026d4 in boost::archive::detail::load_pointer_type<boost::archive::text_iarchive>::invoke<Character*> (ar=..., t=@0x7fffffffdb08: 0x7e0260)
    at /usr/local/include/boost/archive/detail/iserializer.hpp:524
#48 0x000000000040263c in boost::archive::load<boost::archive::text_iarchive, Character*> (ar=..., 
    t=@0x7fffffffdb08: 0x7e0260) at /usr/local/include/boost/archive/detail/iserializer.hpp:592
#49 0x00000000004025e1 in boost::archive::detail::common_iarchive<boost::archive::text_iarchive>::load_override<Character*> (this=0x7fffffffdb10, t=@0x7fffffffdb08: 0x7e0260)
    at /usr/local/include/boost/archive/detail/common_iarchive.hpp:66
#50 0x0000000000402587 in boost::archive::basic_text_iarchive<boost::archive::text_iarchive>::load_override<Character*> (this=0x7fffffffdb10, t=@0x7fffffffdb08: 0x7e0260)
    at /usr/local/include/boost/archive/basic_text_iarchive.hpp:65
#51 0x0000000000402525 in boost::archive::text_iarchive_impl<boost::archive::text_iarchive>::load_override<Character*> (this=0x7fffffffdb10, t=@0x7fffffffdb08: 0x7e0260)
    at /usr/local/include/boost/archive/text_iarchive.hpp:82
#52 0x0000000000402319 in boost::archive::detail::interface_iarchive<boost::archive::text_iarchive>::operator>><Character*> (this=0x7fffffffdb10, t=@0x7fffffffdb08: 0x7e0260)
    at /usr/local/include/boost/archive/detail/interface_iarchive.hpp:60
#53 0x0000000000401869 in loadChar (pCharacter=@0x7fffffffddc0: 0x7dff70) at main.cpp:37
#54 0x00000000004019b5 in main () at main.cpp:51

我已将代码缩减为以下示例:

的main.cpp

#include "Character.h"
#include <fstream>

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

void createCharacter(Character *&pCharacter){
        pCharacter = new Character();
        pCharacter->createCharacter();
}

void saveChar(Character *pCharacter){
    // create and open a character archive for output
    std::ofstream ofs(pCharacter->getName());

    if (ofs.is_open()){
        //FIXME add dialogue to ensure overwriting existant file

        // save data to archive
        boost::archive::text_oarchive oa(ofs);
        // write class instance to archive
        oa << *pCharacter; //segfaults!!
        // archive and stream closed when destructors are called
    } else {
        printf("Unable to open file\n");
    }
}

void loadChar(Character *&pCharacter){
    Character* aChar = new Character();
    string aName = "DEF";
    // create and open an archive for input
    std::ifstream ifs(aName);
    if (ifs.is_open()){
        boost::archive::text_iarchive ia(ifs);
        // read class state from archive
        ia >> aChar;
        // archive and stream closed when destructors are called
    } else {
        printf("Unable to open file\n");
    }
}


int main(){
    Character *aCharacter = nullptr;
    createCharacter(aCharacter);
    saveChar(aCharacter);
    delete aCharacter;
    aCharacter = new Character();
    loadChar(aCharacter);
    return 0;
}

Character.cpp

#include "Character.h"

using std::string;


void Character::createCharacter() {

    setPlayerName();
    setCharacterName();
}

void Character::setPlayerName() {
    mPlayerName = "ABC";
}

void Character::setCharacterName() {
    mName = "DEF";
}

Character.h

#ifndef A_CHAR
#define A_CHAR

#include <string>
#include <vector>
#include <map>
#include <set>

#include <stdlib.h>

// include headers that implement a archive in simple text format
#include <boost/serialization/utility.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/set.hpp>
#include <boost/serialization/access.hpp>
#include <iostream>

using std::string;


class Character {

private:

    string mHomeworldStr;
    string mBackgroundStr;
    string mRoleStr;

    string mName;
    string mPlayerName;

    void setPlayerName();

    void setCharacterName();

    friend class boost::serialization::access;
    // When the class Archive corresponds to an output archive, the
    // & operator is defined similar to <<.  Likewise, when the class Archive
    // is a type of input archive the & operator is defined similar to >>.
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        //FIXME
        ar & mHomeworldStr;
        ar & mBackgroundStr; //breaks serialisation?
        ar & mRoleStr;

    }



public:


    Character() :
            mHomeworldStr("Unset"), mBackgroundStr("Unset"), mRoleStr("Unset"), mName("Unset"), mPlayerName("Unset")
    { /* EMPTY */ }



    // Primary creation method
    void createCharacter();

    const char* getName() { return mName.c_str(); }
    std::string getStringName() { return mName; }
    const char* getPlayerName() { return mPlayerName.c_str(); }

};

#endif

使用SConstruct文件构建:

ccflags = [
    '-Wall',
    '-Werror',
    '-static',
    '-g',
    '--std=c++0x',
    '-pedantic',
]


dhFiles = [
  'main.cpp',
  'Character.cpp'
]

# global link flags go here
linkflags = ['-static']

env = Environment(CCFLAGS=ccflags,LINKFLAGS=linkflags, LIBS=['boost_serialization','pthread'])

env.Program("dh", dhFiles)

对此的任何想法都会失败,我们将不胜感激!

编辑: 这是在ubuntu 15.10上运行boost 1.55,gcc 5.2.1

0 个答案:

没有答案