On Thursday, January 4, 2018 at 4:14:31 AM UTC-8, 기준 wrote:
>
> I'm testing protocol buffers, but encountered segment fault that i can't
> understand.
>
> My protocol buffer is below
>
> ```
> message Sub {
> string value = 1;
> }
>
> message Wrapping {
> int32 key = 1;
> repeated Sub sub = 2;
> }
> ```
>
> Testing code is below
>
> ```
> void Test()
> {
> gbtest::Sub* sub = new gbtest::Sub();
>
> for(int i = 0 ; i<4 ; ++i)
> {
> gbtest::Wrapping wrapping; // Local variable
> wrapping.mutable_sub()->AddAllocated(sub);
>
This will be fine the first time, but "wrapping" will take ownership of
"sub". Since wrapping is destroyed at the end of the loop body, "sub" will
be destroyed also! Then the next time through the loop, it will crash
because sub was deleted.
I don't understand what you are trying to do, so I don't know what fix to
advise.
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.