在Protobuf ParseFromZeroCopyStream()处发生std :: system_error

时间:2019-07-04 03:08:05

标签: c++ ubuntu protocol-buffers zlib

我正在用zlib压缩测试protobuf。

我使用protobuf 3.8.0编写了一些c ++示例代码,但是在Ubuntu上调用ParseFromZeroCopyStream()时发生了以下错误。

terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1
(core dumped)

我该怎么办?

我尝试将ParseFromZeroCopyStream()替换为ParseFromBoundedZeroCopyStream()。 这样不会导致核心转储,但是ParseFromBoundedZeroCopyStream()返回false。

test.proto

syntax = "proto2";

package test;

message Msg
{
    required uint32 data = 1;
}

test.cc

#include <iostream>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#include <google/protobuf/io/gzip_stream.h>
#include "test.pb.h"

using namespace std;
using namespace google::protobuf;
using namespace test;

int main(void)
{
    Msg srcMsg; 
    srcMsg.set_data(1);

    long sSize = srcMsg.ByteSizeLong();
    cout << "SerializedSize = " << sSize << endl;

    char * compressedMsg = new char[sSize];
    io::ArrayOutputStream aos(compressedMsg, sSize); 
    io::GzipOutputStream gos(&aos);

    long cSize;
    if (srcMsg.SerializeToZeroCopyStream(&gos) == true)
    {
        gos.Close();
        cSize = aos.ByteCount();
        cout << "compression success : " << cSize << " bytes" << endl;
    }
    else    
    {
        cout << "compression error" << endl;
        return 1;
    }

    Msg targetMsg;

    io::ArrayInputStream ais(compressedMsg, cSize); 
    io::GzipInputStream gis(&ais);

    if (targetMsg.ParseFromZeroCopyStream(&gis) == false)
    {
        cout << "decompression error" << endl;
    }
    else    
    {
        cout << "decompression success : " << targetMsg.ByteSizeLong() << " bytes" << endl;
        cout << "data = " << targetMsg.data() << endl;
    }

    delete[] compressedMsg;

    return 0;
}

我希望减压成功。

1 个答案:

答案 0 :(得分:0)

如果可能的话,您将需要学习使用调试器来进一步调查为什么抛出此“未知错误:-1”。

话虽这么说,未知的库错误是 有时 ,它是由内存分配失败或在极少数情况下,其他一些资源约束(如无法启动线程/进程等)引起的

相关问题