Is this even a legal transformation that the MS compiler is doing
there ? Sounds like a overzealous-optimization compiler bug to me.

Did you try to manually remove the 'const' in the generated proto code
to see if that would fix it ?

On Wed, Nov 10, 2010 at 18:51, Anand Ganesh
<anantharamangan...@gmail.com> wrote:
> Hi Kenton, all,
>
> When you have a message which contains a string member variable, the
> variable is initialized to a default-value.
>
> So you have
> ::std::string *name_;
> static const ::std::string _default_name_;
> name_ = &_default_name;
>
> Seems like since _default_name is declared 'const' the microsoft
> compiler is optimizing for cache localization and putting
> _default_name_ inside each individual object (like it would for a
> member variable). Hence _default_name_ has different addresses in two
> different objects. Functionally this is not a problem because there's
> no data-sharing (as in, one object modifying the static variable to a
> new value and others reading the new value from it) because it is
> declared const.
>
> What this is doing is that when you Swap() two objects with the 'name'
> member variable, where one object is at the default value but the
> other is not, the destructor causes a core-dump. Let me explain better
> using the following workflow:
>
> ObjectA:
> name_ = 0X123 ""
> (0x123) _default_name_ = ""
>
> ObjectB:
> name_ = 0x456 ""
> (0x456) _default_name_ = ""
>
> ObjectA.set_name("Foo");
> ObjectA:
> name_ = 0xABC "Foo"
> (0x123) _default_name_ = ""
>
> /* Lets see what happens when you Swap() */
> ObjectB.Swap(ObjectA);
> ObjectA:
> name_ = 0x456 ""
> (0x123)_default_name_ = ""
>
> ObjectB:
> name_ = 0xABC "Foo"
> (0x456) _default_name_ = ""
>
> delete ObjectB; // good!
> delete ObjectA; // crash! Because name_ != &_default_name_, delete
> name_ is invoked.
>
> This is not a problem on Linux because even though _default_name_ is a
> static const, it seems the Linux compiler is optimizing for space and
> adding it to a global space and all objects' name_ values point to the
> same address.
>
> Comments? Anything that can be done on Windows to fix this?
>
> Regards,
> Anand
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Protocol Buffers" group.
> To post to this group, send email to proto...@googlegroups.com.
> To unsubscribe from this group, send email to 
> protobuf+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/protobuf?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to proto...@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to