分配消息崩溃在google protobuf
Allocated Message crash in google protobuf
我有一个崩溃使用protobuf,我需要使用它与反射。
enum ipVersionType{
ipv4 = 0;
ipv6 = 1;
}
message IpAddress {
required ipVersionType ipVersion = 1;
required uint32 IpPart1 = 2;
required uint32 IpPart2 = 3;
required uint32 IpPart3 = 4;
required uint32 IpPart4 = 5;
}
message TcpUdpCdr {
...
optional IpAddress DestinationIp = 8;
optional IpAddress UEIP = 11;
optional uint32 PacketUpLink = 12;
....
}
message cdr {
optional TcpUdpCdr tcpCdr = 1;
}
当我使用没有cdr的tcppcdr时,我没有崩溃。
如果我在cdr中使用TcpUdpCdr,我就会崩溃。
这是我用来设置Ipaddress
的代码//Fill proto ip address sruct
ProtoCdr::IpAddress * ipAddressMsg = new ProtoCdr::IpAddress();
ipAddressMsg->set_ipversion(ProtoCdr::ipVersionType::ipv4);
ipAddressMsg->set_ippart1(pi_ipAddress.GetAddresPointer()[0]);
ipAddressMsg->set_ippart2(pi_ipAddress.GetAddresPointer()[1]);
ipAddressMsg->set_ippart3(pi_ipAddress.GetAddresPointer()[2]);
ipAddressMsg->set_ippart4(pi_ipAddress.GetAddresPointer()[3]);
google::protobuf::Message& find_msg = cdrMsg.GetReflection()->GetMessage... with local recursive function
find_msg is of type TcpUdpCdr
find_msg.GetReflection()->SetAllocatedMessage(
&find_msg,
ipAddressMsg,
this->m_fdArray[m_iNestingSize-1]);
到这里为止,没有任何崩溃…
如果我尝试用GetReflection GetMessage获得指针,我收到与SetAllocatedMessage设置的相同的指针地址,但当我尝试使用它时,它崩溃了。第二个IpAddress UEIP会崩溃,但第一个不会崩溃....
问题是您使用SetAllocatedMessage
将ipAddressMsg
的所有权分配给find_msg
,但ipAddressMsg
已经由cdrMsg
拥有。因为ipAddressMsg
现在由两个不同的protos拥有,你很快就会遇到问题,因为两个所有者最终都会试图删除它。我认为你的代码试图使用已经被删除的子消息。
请注意,通常对于protobuf,包含release
或allocated
字样的方法是一种高级功能——它们可以提高性能,但它们增加了复杂性,并且需要您进行更多的手动内存管理。使用这些方法很容易意外地遇到内存泄漏、双重删除和删除后使用。除非你真的需要这种表现,否则最好避免使用它们。在您的情况下,您可以将结果赋值为MutableMessage()
,而不是调用SetAllocatedMessage()
。
相关文章:
- Google protobuf 时间戳未声明标识符,在 Windows 上具有C++
- E/libc++abi:终止于类型为google::protobuf::FatalException的未捕获异常
- protobuf in C++ with dynamic binding for google::protobuf::M
- Google protobuf and Android NDK
- 防止客户端套接字程序崩溃CPP Google Protobuf
- 致命错误:google/protobuf/port_def.inc:没有这样的文件或目录 #include < google/protobuf/port_def.inc>
- 静态地将Google Protobuf lib链接到DLL库中
- 对Google :: Protobuf的未定义引用在ROS CPP中
- Google Protobuf 3:使用 CMAKE 构建时未定义的引用错误
- 通过Google Protobuf发送二进制文件数据
- 对Google :: ProtoBuf :: Internal :: empty_string_ [abi:cxx11]
- Google Protobuf基于C++的python实现的性能
- Google protobuf在解析字符串数据时在Android中崩溃
- C++Protobuf错误google::Protobuf::internal::kEmptyString错误
- 如何使用Google Protobuf实现地图结构
- 带有协议缓冲区的程序无法使用 MinGW-w64 编译:"undefined reference to google::protobuf:: ..."
- 无法在VS2015中正确链接Google Protobuf
- 从部分google protobuf消息中检索信息
- Google ProtoBuf序列化/反序列化
- encodeffloat在Google Protobuf中是如何工作的?