https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64830

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This is not a bug in GCC.

tl;dr https://isocpp.org/wiki/faq/ctors#static-init-order


The segfault is due to dereferencing default_instance_ here:

inline const ::BundleConfigInfo& ConfigInfo::bundle() const {
  return bundle_ != NULL ? *bundle_ : *default_instance_->bundle_;
}

That is supposed to be initialized by protobuf_AddDesc_testcase_2eproto() which
gets called from:

// Force AddDescriptors() to be called at static initialization time.
struct StaticDescriptorInitializer_testcase_2eproto {
  StaticDescriptorInitializer_testcase_2eproto() {
    protobuf_AddDesc_testcase_2eproto();
  }
} static_descriptor_initializer_testcase_2eproto_;

Which is a global object in testcase.pb.cc

Your testcase.cc also has a global object:

namespace ConfigHelper
{
    Config defaultConfig; // causes segfault
}

The order of initialization of globals is undefined in separate translation
units, so you are wrong to assume that the protobuf global will be initialized
before your global.

I was able to avoid the segfault by simply reordering the files passed to the
linker, so that testcase.pb.o comes before testcase.o, but that is still
relying on something that is not guaranteed.

Either protobuf should find a better way to initialize its defaults or you
should not use it from globals.

Reply via email to