why don't Readers have copy constructors?

545 views
Skip to first unread message

Jason E. Aten

unread,
Oct 19, 2013, 3:24:18 PM10/19/13
to Kenton Varda, capnproto
In the code generated by capnp from a pp.capnp file, it seems that Readers are returned by value, and so should have copy-constructors. But the pp.capnp.h generated file doesn't appear to declare copy constructors for Readers... so I'm getting the compiler-written default-shallow copy constructor.

I'm getting crashes from lack of deep copy as a result...

See common_pp.cpp:86 in the attached minimized test case.


pointerproblem.tar.gz

Kenton Varda

unread,
Oct 19, 2013, 5:47:08 PM10/19/13
to Jason E. Aten, capnproto
Hi Jason,

This is working as intended.

As described in the docs, Readers and Builders both behave like pointers -- they point at something else, but they don't own the data.  Copying a Reader is like copying a pointer; it's a shallow copy.

Doing a deep copy is complicated because Cap'n Proto objects are not allocated on the heap.  You need to copy *to* some other message.  What you can do is something like:

    MallocMessageBuilder newMessage;
    newMessage.setRoot(someReader);

This will make a deep copy of someReader, making it the root of newMessage.  Similarly, struct-typed fields have "set" accessors that make a deep copy.

-Kenton

Jason E. Aten

unread,
Oct 19, 2013, 6:54:28 PM10/19/13
to Kenton Varda, capnproto
Hi Kenton,

I'm still puzzled, then, as to why I am seeing the crash at line 67 of clinano.cpp.  If Reader is just a pointer, then this should succeed, but it crashes with:

terminate called after throwing an instance of 'kj::ExceptionImpl'                                                                                           
  what():  src/capnp/layout.c++:1904: requirement not met: expected boundsCheck(segment, ptr, ptr + roundBytesUpToWords(ref->listRef.elementCount() * (1 * BY\
TES / ELEMENTS))); Message contained out-of-bounds text pointer.   

- Jason

Kenton Varda

unread,
Oct 19, 2013, 6:58:59 PM10/19/13
to Jason E. Aten, capnproto
It looks like receiveZsample() is returning a Reader that points into a message that no longer exists after the function returns.  At least, I don't see any MessageBuilder instances allocated on the stack.

Jason E. Aten

unread,
Oct 19, 2013, 7:00:43 PM10/19/13
to Kenton Varda, capnproto
Ah, I think I see. The FlatArrayMessageReader at line 73 in common_pp.cpp seems to "own" the message,because things don't crash if I new it up instead of making the FlatArrayMessageReader stack allocated.

In general is it correct to surmise that the MessageBuilder or the FlatArrayMessageReader will "own" the messages?

Kenton Varda

unread,
Oct 19, 2013, 7:02:15 PM10/19/13
to Jason E. Aten, capnproto
Yes, the MessageBuilder/MessageReader is the owner of the message content.

Jason E. Aten

unread,
Oct 19, 2013, 7:08:29 PM10/19/13
to Kenton Varda, capnproto
On Sat, Oct 19, 2013 at 4:02 PM, Kenton Varda <temp...@gmail.com> wrote:
Yes, the MessageBuilder/MessageReader is the owner of the message content.

Very good -- thanks for clarifying that.

Btw, I'm using nanomsg presently, but would be happy to try your rpc if you would benefit from some early testing on it.

Kenton Varda

unread,
Oct 21, 2013, 3:46:35 PM10/21/13
to Jason E. Aten, capnproto
Hopefully Cap'n Proto RPC will be ready within a week or two...
Reply all
Reply to author
Forward
0 new messages