分配消息崩溃在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会崩溃,但第一个不会崩溃....

问题是您使用SetAllocatedMessageipAddressMsg的所有权分配给find_msg,但ipAddressMsg已经由cdrMsg拥有。因为ipAddressMsg现在由两个不同的protos拥有,你很快就会遇到问题,因为两个所有者最终都会试图删除它。我认为你的代码试图使用已经被删除的子消息。

请注意,通常对于protobuf,包含releaseallocated字样的方法是一种高级功能——它们可以提高性能,但它们增加了复杂性,并且需要您进行更多的手动内存管理。使用这些方法很容易意外地遇到内存泄漏、双重删除和删除后使用。除非你真的需要这种表现,否则最好避免使用它们。在您的情况下,您可以将结果赋值为MutableMessage(),而不是调用SetAllocatedMessage()

相关内容

最新更新