On Wed, Dec 11, 2013 at 3:03 AM, <chen...@gmail.com> wrote:

> Dears,
> 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?
>
I think this is a bug in protobuf code. Nonetheless, you'd better avoid
using global variables of class type. Such usage is explicitly forbidden by
Google C++ style guide for exactly the problem you see here.


>  --
> 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 protobuf+unsubscr...@googlegroups.com.
> To post to this group, send email to protobuf@googlegroups.com.
> Visit this group at http://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
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 protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to