2012-03-04 8 views
2

boost::archive::binary_oarchiveを使用している問題があります。プログラムを実行すると、ia >> boost::serialization::make_binary_object(buffer, size)をインスタンス化するときにプログラムがクラッシュする。それが動作boost::archive::text_oarchiveで ...boost :: archive :: binary_oarchive = program crash?

#include <boost/archive/binary_oarchive.hpp> 
#include <boost/archive/binary_iarchive.hpp> 
#include <boost/serialization/string.hpp> 
#include <boost/serialization/binary_object.hpp> 
#include <iostream> 
#include <fstream> 
using namespace std; 
void save() 
{ 
    size_t size = 0;  
    std::ifstream infile("any_file.png", std::ios::in | std::ios::binary | std::ios::ate); 
    if (infile.is_open()) 
    { 
     size = infile.tellg(); 
     char *buffer = new char[size]; 
     infile.seekg(0, ios::beg); 
     infile.read(buffer, size); 
     infile.close(); 

     std::ofstream file("archiv.bin"); 
     boost::archive::binary_oarchive oa(file); 
     oa << size; 
     oa << boost::serialization::make_binary_object(buffer, size); 
     file.close(); 

     delete [] buffer; 
    } 
} 

void load() 
{ 
    size_t size = 0; 
    std::ifstream file("archiv.bin"); 
    boost::archive::binary_iarchive ia(file); 

    ia >> size; 
    char *buffer = new char[size]; 
    ia >> boost::serialization::make_binary_object(buffer, size); //program crash 
    file.close(); 

    ofstream outfile("any_file_out.png", ios::out | ios::binary); 
    for(size_t i = 0; i < size; i++) 
    { 
     outfile << buffer[i]; 
    } 
    outfile.close(); 
    delete [] buffer; 
} 

int main() 
{ 
    save(); 
    load(); 
    return 0; 
} 

は、事前にありがとうございます!

編集: これはどのように動作するのですか。

... 
std::ofstream file("archiv.bin", ios_base::binary); 
... 
std::ifstream file("archiv.bin", ios_base::binary); 
... 
+0

WorksForMe™は、 'any_file.png'と' any_file_out.png'のmd5sumsをチェックしました。私はあなたがコンパイル/リンクしているlibs /ヘッダーと実行時にロードされたバージョンとの間のバージョンの競合だと確信しています。 – sehe

答えて

1

ソリューション自体を提示:)

... 
std::ofstream file("archiv.bin", ios_base::binary); 
... 
std::ifstream file("archiv.bin", ios_base::binary); 
... 
完全に機能するようになりました