protobuf在使用时出现的问题小汇总

  文章开头放一下IBM的google protocol buffer的介绍连接。http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/linux

  对于这种技术不断更新的东西,我只能这么理解:由于技术在更新,版本在变化,以前很详尽的技术博文可能在以后的版本中遇到没法预知的问题,因此咱们在使用时要注意甄别。废话很少说,进入正文部分。ios

  安装什么的都是小问题了,仍是贴一下步骤吧。c++

 tar -xzf protobuf-2.5.0.tar.gz 
 cd protobuf-2.5.0 
 ./configure --prefix=$INSTALL_DIR 
 make 
 make check 
 make install

  很简单的标准*nix的安装方法。随后将安装目录的bin目录下的protoc建立一个软链接到/usr/bin目录下便可。redis

  随后能够进行简单的例子测试了。我这里有一个测试例子:函数

file:base.taskinfo.proto工具

package base;

message funcinfo
{
        required        string  funchandle = 1;//处理函数列表
        optional    string  funcinit = 2;   //初始化函数
        optional    string  funcclean = 3;  //清理函数
}

message taskinfo
{
        required        int32   taskid = 1;             //任务id
        required        int32   pidcount = 2;   //进程数目
        required        string  filepath = 3;   //输入文件路径
        required        string  filename = 4;   //文件名
        required        string  temppath = 5;   //临时文件路径
        required        string  scriptpath = 6; //脚本路径
        required        funcinfo func = 7;              //函数信息
        required string redisconn = 8;  //redis连接ip及port
}

  而后用它来生成c++类型的相对应的文件:protoc --cpp_out=./ base.taskinfo.proto测试

  就生成了两个标准c++的头文件和源文件:base.taskinfo.pb.cc  base.taskinfo.pb.hui

  而后就能够写简单的例子进行测试了,这里写了一个叫write.cpp的简单例子:(如下****部分涉及我的隐私,避免被人肉)this

#include "base.taskinfo.pb.h"
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
        base::funcinfo func;
        func.set_funchandle("GetKeyVal");

        base::taskinfo msg1;
        msg1.set_taskid(1);
        msg1.set_pidcount(3);
        msg1.set_filepath("/data/home/****/data/basetest/indir");
        msg1.set_filename("CH_PW_WAY");
        msg1.set_temppath("/data/home/****/data/basetest/tmpdir");
        msg1.set_scriptpath("/data/home/****/data/basetest/perl/CH_PW_WAY.pl");
        msg1.set_allocated_func(&func);
        msg1.set_redisconn("10.200.25.155:7379");
        fstream output("./log", ios::out | ios::trunc | ios::binary);
        if (!msg1.SerializeToOstream(&output)) {
                cerr<<"Failed to write msg."<< endl; 
                return -1;
        }
        return 0;
}

  而后就能够写个Makefile去编译这1个cpp,1个cc和1和h文件(protoc生成的.cc文件)google

#makefile by ****
#2015年1月4日09:07:28
cc = g++
cpp = -fPIC -Wall -g

inc += -I./
inc += -I/soft/protobuf-2.5.0/include

libs += -L/soft/protobuf-2.5.0/lib -lprotobuf -lpthread

objs=$(patsubst %.cc,%.o, $(wildcard *.cc))
objs+=$(patsubst %.cpp,%.o, $(wildcard *.cpp))
%.o:%.cc
        $(cc) $(cpp) $(inc) -o $@ -c $<
        @echo "$@"

%.o:%.cpp
        $(cc) $(cpp) $(inc) -o $@ -c $<
        @echo "$@"

module=./writer

all:$(module)

$(module):$(objs)
        $(cc) $(cpp) $(inc) -o $(module) $^ $(libs)
        @echo "$@"

clean:
        @rm -f $(objs) $(module)
        @echo "clean done."

编译没问题,而后很快乐的就生成了可执行文件

随后就是 执行,让他生成log文件就好了,执行的时候出现了没法找到lib的错误,好吧,把刚才的安装目录下的lib目录添加到环境变量$LD_LIBRARY_PATH中去,再执行就出现了:

*** glibc detected *** ./writer: munmap_chunk(): invalid pointer: 0x00007fff8521b9b0 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x75358)[0x7fa138d93358]
./writer[0x4046f1]
./writer[0x403732]
./writer[0x4044ae]
./writer[0x409010]
/lib64/libc.so.6(__libc_start_main+0xe6)[0x7fa138d3cc36]
./writer[0x403099]
======= Memory map: ========

..如下省略数千个字符。总之就是core掉了。试试gdb工具,调试结果以下所示:

Missing separate debuginfo for /lib64/ld-linux-x86-64.so.2
Try: zypper install -C "debuginfo(build-id)=c81de241a528795f8dbfde0f0e0e236f9a6554e6"
Core was generated by `./writer'.
Program terminated with signal 6, Aborted.
#0  0x00007fad42931b55 in raise () from /lib64/libc.so.6
(gdb) bt
#0  0x00007fad42931b55 in raise () from /lib64/libc.so.6
#1  0x00007fad42933131 in abort () from /lib64/libc.so.6
#2  0x00007fad4296ec2f in __libc_message () from /lib64/libc.so.6
#3  0x00007fad42974358 in malloc_printerr () from /lib64/libc.so.6
#4  0x00000000004046e1 in base::funcinfo::~funcinfo (this=0x7fff3026a2c0, __in_chrg=<optimized out>) at base.taskinfo.pb.cc:167
#5  0x0000000000403722 in base::taskinfo::SharedDtor (this=0x7fff3026a270) at base.taskinfo.pb.cc:537
#6  0x000000000040449e in base::taskinfo::~taskinfo (this=0x7fff3026a270, __in_chrg=<optimized out>) at base.taskinfo.pb.cc:517
#7  0x0000000000409000 in main () at writer.cpp:25

应该是析构函数出错了,看一下到底protoc给咱们生成了什么东西吧。这段代码摘自base.taskinfo.pb.cc

void taskinfo::SharedDtor() {
  if (filepath_ != &::google::protobuf::internal::kEmptyString) {
    delete filepath_;
  }
  if (filename_ != &::google::protobuf::internal::kEmptyString) {
    delete filename_;
  }
  if (temppath_ != &::google::protobuf::internal::kEmptyString) {
    delete temppath_;
  }
  if (scriptpath_ != &::google::protobuf::internal::kEmptyString) {
    delete scriptpath_;
  }
  if (redisconn_ != &::google::protobuf::internal::kEmptyString) {
    delete redisconn_;
  }
  if (this != default_instance_) {
    delete func_;
  }
}

我擦。(请原谅个人粗鲁),竟然是delete func_,我顿时就不能淡定了。我申请的是栈内存,你给我来个delete,不core才怪了~好吧,请原谅我没有看你这个生成的1061行的源代码。

改一下上面的write.cpp的代码:

#include "base.taskinfo.pb.h"
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
        base::funcinfo *func = new base::funcinfo();
        func->set_funchandle("GetKeyVal");

        base::taskinfo *msg1 = new base::taskinfo();
        msg1->set_taskid(1);
        msg1->set_pidcount(3);
        msg1->set_filepath("/data/home/****/data/basetest/indir");
        msg1->set_filename("CH_PW_WAY");
        msg1->set_temppath("/data/home/****/data/basetest/tmpdir");
        msg1->set_scriptpath("/data/home/****/data/basetest/perl/CH_PW_WAY.pl");
        msg1->set_allocated_func(func);
        msg1->set_redisconn("10.200.25.155:7379");
        fstream output("./log", ios::out | ios::trunc | ios::binary);
        if (!msg1->SerializeToOstream(&output)) {
                cerr<<"Failed to write msg."<< endl; 
                return -1;
        }
        return 0;
}

好 编译运行没问题了,都解决了。生成了一个log文件,这文件没办法看了。内容按照他本身的格式写的,有一些乱码和一些能看得懂的,本身写进去的东西。

相关文章
相关标签/搜索