【发布时间】:2017-04-20 08:23:49
【问题描述】:
我必须通过boost::shared_ptr:
boost::shared_ptr<Protobuf::Person::Profile> pProfile =
boost::make_shared<Protobuf::Person::Profile>();
这是 protobuf 的指针,指向 protobuf 的函数 oPerson.set_allocated_profile(pProfile) 但 oPerson.set_allocated() 需要一个指向 Protobuf::Person::Profile 的指针。
我尝试了几种方法,但我认为当我尝试使用 pbjson::pb2Json(一个基于快速 json 构建的库函数)将 protobuf 对象转换为 JSON 时,指针超出范围导致分段错误。
方法一:
oPerson.set_allocated_profile(pProfile.get());
方法二:
oPerson.set_allocated_profile(&*pProfile);
【问题讨论】:
-
在没有更多上下文的情况下很难提供帮助:检查您的生命周期并确保内存在使用时仍然有效。
标签: c++ boost shared-ptr rapidjson make-shared