I am trying to reconstruct received data using Capnproto, but am getting 
the following error.

    terminate called after throwing an instance of 'kj::ExceptionImpl'
      what():  capnp/arena.c++:76: failed: expected 
reinterpret_cast<uintptr_t>(segment.begin()) % sizeof(void*) == 0 [4 == 0]; 
Detected unaligned data in Cap'n Proto message. Messages must be aligned to 
the architecture's word size. Yes, even on x86: Unaligned access is 
undefined behavior under the C/C++ language standard, and compilers can and 
do assume alignment for the purpose of optimizations. Unaligned access may 
lead to crashes or subtle corruption. For example, GCC will use SIMD 
instructions in optimizations, and those instrsuctions require alignment. 
If you really insist on taking your changes with unaligned data, compile 
the Cap'n Proto library with -DCAPNP_ALLOW_UNALIGNED to remove this check.
    stack: 7f4233c40ca5 7f423bbc40cf6 7f42bbccaa27 55f370c02321 
55f770cc18e3 55f77ac01331 55f77a0b12da 55f7a0cb127b 55fc70c0d1ed 
55fe70c01137 7f42bffa7de3 7f42b5cc7608 7f42b5aa6292

It is from this snippet where I am receiving the buffer and trying to 
rebuild the data structure.

    const void* buff = getBuff();
    
    auto arr = kj::ArrayPtr<const capnp::word>(
        reinterpret_cast<const capnp::word*>(buff),
        size / sizeof(capnp::word));

    capnp::FlatArrayMessageReader msgReader(arr);

    auto msg = msgReader.getRoot<MyStuff>(); // error occurs at this line
    auto fieldA = msg.getFieldA();
    auto fieldB = msg.getFieldB();
    

The buffer was constructed and sent using:

    capnp::MallocMessageBuilder message;
    auto stuff = message.initRoot<MyStuff>();
    stuff.setFieldA("A");
    stuff.setFieldB("B");
    
    const auto msgToSend = capnp::messageToFlatArray(message);
    const auto msgAsChars = msgToSend.asChars();

    // make const void* and send

How do I align the data to the architectures word size as the error says?

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/capnproto/c6755765-f1f3-4de6-af29-dfdb2bb83881n%40googlegroups.com.

Reply via email to