On Wed, Nov 10, 2010 at 6:51 PM, 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.
>

This doesn't make sense.  _default_name_ is only defined once, in the .pb.cc
file.  The compiler should only be compiling that file once, and therefore
should only be producing one instance of the object.  Other translation
units which #include your .pb.h cannot possibly have their own definition of
_default_name_ because they don't know what it should be initialized to.

So the only way this is possible is if you are actually linking together two
separate instances of the same .pb.o file.  Perhaps you have statically
linked your .pb.o into two different DLLs, and you are then trying to pass
objects between them?  This will not work -- they need to share a single
instance of the object file.  This problem is not unique to protocol
buffers; it is generally unsafe to link two copies of the same class into
one process and expect them to be compatible.

You will either have to link the .pb.o file into a DLL that can be shared by
the other two, or (probably safer) you could serialize the message before
passing it across the DLL boundary and then parse it again on the other
side.  See also the warnings about DLLs in vsprojects/readme.txt.

-- 
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