Hi Sune, In what order are m_message and m_myMsgBuilder declared in the class? My guess is that m_myMsgBuilder is declared before m_message. As a result, the compiler will re-order the initializations such that m_myMsgBuilder is initialized first. This means your code is calling m_message.initRoot when m_message is not yet constructed, resulting in undefined behavior. (Most compilers will give you a warning about this...)
-Kenton On Wed, Sep 4, 2019 at 7:04 PM Sune S <[email protected]> wrote: > > Hi > > I have a Class that instantiates a MallocMessageBuilder and initializes > its root to a custom struct. I notice a segfault when I construct the > custom struct's builder in the constructor's initializer list. > > *// Construction that leads to a seg fault* > > MyMessageBuilder() > > : m_message(), > > m_myMsgBuilder(m_message.initRoot<MyMessage>()) > > { > > // The program seg faults in the following line. > > m_myMsgBuilder.initSomeStruct(); > > } > > > *// Alternate construction does not crash* > > MyMessageBuilder() > > : m_message(), > > m_myMsgBuilder(nullptr) > > { > > m_myMsgBuilder = m_message.initRoot<MyMessage>(); > > m_myMsgBuilder.initSomeStruct(); > > } > > > 0x00000000004059a0 in > capnp::_::DirectWireValue<MyMessage::Which>::set(MyMessage::Which) () > > 0x00000000004056fe in void > capnp::_::StructBuilder::setDataField<MyMessage::Which>(unsigned int, > kj::NoInfer_<MyMessage::Which>::Type) () > > 0x000000000040551d in MyMessage::Builder::initSomeStruct() () > > > What could be causing the seg fault? > > > Thanks > > Sune > > -- > You received this message because you are subscribed to the Google Groups > "Cap'n Proto" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/capnproto/23b59bc8-bfa3-4e3d-964d-7bfdf2fca5fe%40googlegroups.com > <https://groups.google.com/d/msgid/capnproto/23b59bc8-bfa3-4e3d-964d-7bfdf2fca5fe%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "Cap'n Proto" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/CAJouXQ%3D2H0quQjiVkmf1cPAgJdO%2Bhav78_Fn-2o9OCj_ANUPZQ%40mail.gmail.com.
