I have a test.proto file like this:

message Test {
    optional string str_1 = 1;
    optional string str_2 = 2 [default="3.0"];

A default value is set for string field str_2. In the main file I defined a 
global Test instance and access str_2 in main function like this:

#include "..."

Test g_test;
int main(int argc, char** argv) {
    cout << g_test.str_2().c_str() << endl;  // This should output "3.0" 
but I got sigsegv under some situations.

As the comment suggested, I got sigsegv under certain situations. I looked 
into the code generated by protoc version 2.5, in the constructor of Test, 
str_2 was pointed to _default_str_2_, which is NULL at the beginning and 
initialized by another global variable(named 
static_descriptor_initializer_test_2eproto_)'s constructor. 
So, if the global variable defined by me (g_test) was initialized 
after static_descriptor_initializer_test_2eproto_, the above code would 
work as expected. However, if static_descriptor_initializer_test_2eproto_ 
is initialized after g_test, then g_test.str_2 will point to NULL and cause 
sigsegv when accessed.

Is this an intended behavior? Which means I should avoid using global 
instance of message that have a string field with default value?

You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
To post to this group, send email to
Visit this group at
For more options, visit

Reply via email to