Segfault while using Builder

27 views
Skip to first unread message

Sune S

unread,
Sep 4, 2019, 10:04:17 PM9/4/19
to Cap'n Proto

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

Kenton Varda

unread,
Sep 4, 2019, 11:35:43 PM9/4/19
to Sune S, Cap'n Proto
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

--
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 capnproto+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/23b59bc8-bfa3-4e3d-964d-7bfdf2fca5fe%40googlegroups.com.

Sune S

unread,
Sep 4, 2019, 11:51:38 PM9/4/19
to Cap'n Proto
Hi Kenton,

You were right, it was the order of initialization. I should have checked that first.  I have now added the all warnings option to the compiler.

Thank you.

Sune.
To unsubscribe from this group and stop receiving emails from it, send an email to capn...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages