1

I'm newbie in google protocol buffer. And now I have a issue as below:

I have created a simple message in testMessage.proto file:

option optimize_for = SPEED;

message TestMessage
{
    optional string foo  = 1;
    optional string bar = 2;
}

Then I generated this message to testMessage.pb.h and testMessage.pb.cc files and included these files, also added libprotobuf libs to my test project. Then I wrote a simple code to use this class:

TestMessage testMsg; 
testMsg.set_foo("1234"); // set ok
testMsg.set_bar("abcd"); // set ok 
string output; 
try {   
    std::cout << testMsg.foo() << std::endl; // output foo ok
    testMsg.PrintDebugString();  // throw bad allocation exception here
    // testMsg.SerializeToString(&output); // also throw bad allocation exception here
 } catch (std::exception ex) {  
     std::cout << ex.what() << std::endl; 
 }

This code is very simple, but I cannot understand why it cannot run correctly. I googled for my issue but there is nowhere mention about it. Everyone please help me with this issue.

Trung Nguyen
  • 818
  • 2
  • 10
  • 17
  • What is `locator`? – Oliver Charlesworth Apr 16 '17 at 09:25
  • @OliverCharlesworth Sorry, Just a copy paste error. – Trung Nguyen Apr 16 '17 at 09:26
  • How about inspecting a call stack when this exception is getting thrown? – user7860670 Apr 16 '17 at 10:09
  • @VTT `void protobuf_AssignDesc_testMessage_2eproto() { protobuf_AddDesc_testMessage_2eproto(); const ::google::protobuf::FileDescriptor* file = ::google::protobuf::DescriptorPool::generated_pool()->FindFileByName( "testMessage.proto"); // exception thrown here, in file testMessage.pb.cc` – Trung Nguyen Apr 16 '17 at 10:20
  • And it probably contains some condition that causes exception to be thrown, right? – user7860670 Apr 16 '17 at 10:36
  • Can you include the command line you are using to compile the proto file, and to compile and link your test program? (I wonder if you're failing to link the generated code. And in any case you should be providing a [mcve].) – Alan Stokes Apr 16 '17 at 10:37
  • @AlanStokes, I used command: protoc -I=my_src_dir --cpp_out=my_dst_dir *.proto to generate code from proto files. And I copied libprotobuf.lib file and placed in the Debug file to use protobuf library. And I am using protocol buffer 2.6.0. – Trung Nguyen Apr 16 '17 at 11:14
  • @VTT I just see this in Locals dialog. - file 0xcccccccc {name_=??? package_=??? pool_=??? ...} const google::protobuf::FileDescriptor *. And nothing else. – Trung Nguyen Apr 16 '17 at 11:19
  • Did you compile and link the code generated in my_dst_dir into your program? – Alan Stokes Apr 16 '17 at 14:29
  • @Alan Stockes, Yes, I did it. My program compiled ok, the exception just happen when I run the program. – Trung Nguyen Apr 17 '17 at 02:18
  • Updated: there are some issues happen during compiling, I tried to rebuild everything again then it worked normal now. – Trung Nguyen Mar 21 '18 at 02:05

0 Answers0