c++ - Setting a Protobuf Submessage or Embeded Message and return the Message Object -
i run segfault when using protocol-buffers submessages. can't post code try give example.
the .proto code this:
message { optional int id = 1; optional submessage submsg = 2; } submessage { optional int foo = 1; }
in c++ code have function creates , returns submessage other function embeds submessage message , returns 1 other function,...
submessage createsubmessage() { submessage smsg; smsg.set_foo(12); return smsg; } message createmessage() { message msg; // line segfault, without not submessage* smsg = msg.mutable_submsg(); // segfault no matter if // set submessage or not. // smsg.copyfrom(createsubmessage(); return msg; } void foo() { for(;;) { message msg; msg = createmessage(); } }
and error getting coming submessage::mergefrom()
[libprotobuf fatal messges_pb.cc:1128] check failed: (&from) != (this): segmentation fault
the segfault interestingly appears in second iteration of loop, @ second assignment msg.
i tried lot of fuzzing, allocating submessage on heap , setting set_allocated_submsg(), copyfrom(), mergefrom(), copy constructors , on,... still running same problem.
i think var msg in function createmessage() destroyed , disallocated after return of function.
you should try use msg attribut of main class.
Comments
Post a Comment