Kenton,
A quick follow-up on the question above. What's the life of the heapArray?
In other word when does the heapArray get destructed? In my application, I
have to store the heapArray in a global vector to use later for
serialization (possibly on a different thread), which will eventually be
stored in a DB as a byte array/blob. A very high level implementation would
look something like this:
using BinarySessionData = std::vector<kj::ArrayPtr<kj::
byte>>;
BinarySessionData encodedSessionData;
void encode(capnp::MessageBuilder* message)
{
kj::Array<capnp::word> flatArray = capnp::messageToFlatArray(*
message);
encodedSessionData[i] = flatArray.asBytes();
}
Let's assume the vector is in global address space. My question is will the
heapArray get destroyed once it goes out of scope of the encode() function?
If not, when will it be deleted? If so, short of memcpy'g is there a better
way to cache the encoded output?
Thanks,
Kaushik Agrawal.
On Wednesday, September 30, 2015 at 5:12:44 PM UTC-4, Zach La Celle wrote:
>
> Thank you for the reply. I ended up figuring out the ToFlatArray
> interface, but the tip about using heapArray vs trying to do my own Array
> is helpful.
>
> On Wednesday, September 30, 2015 at 4:59:01 PM UTC-4, Kenton Varda wrote:
>>
>> Hi Zach,
>>
>> kj::Array represents an array allocated on the heap. The size is
>> specified at allocation time and cannot change after that. It looks like
>> you're expecting it to behave like a growable vector.
>>
>> The constructor for kj::Array that you seem to be using is meant only for
>> advanced users who want to play memory tricks. The normal way to allocate
>> an array is to use kj::heapArray<byte>(size).
>>
>> But probably you should try a completely different approach:
>>
>> kj::Array<capnp::word> words =
>> capnp::messageToFlatArray(messageBuilder);
>> kj::ArrayPtr<kj::byte> bytes = words.asBytes();
>>
>> -Kenton
>>
>> On Wed, Sep 30, 2015 at 11:20 AM, <[email protected]> wrote:
>>
>>> I'm trying to learn CapnProto 0.5.3, and I'm having issues writing
>>> messages to memory instead of to file descriptors (which works fine).
>>>
>>> 1) Should I use Array or ArrayBuilder?
>>> 2) How do I initialize an Array properly?
>>> 3) Why use Array vs FixedArray?
>>>
>>> My code which I've tried to do is as follows:
>>> int serializedSizeWords =
>>> ::capnp::computeSerializedSizeInWords(messageBuilder);
>>>
>>> ::capnp::byte byte1 = 0x00;
>>> kj::Array<::capnp::byte> array(&byte1, serializedSizeWords,
>>> ::kj::DestructorOnlyArrayDisposer::instance);
>>> kj::ArrayPtr<::capnp::byte> arrayPtr = array.asPtr();
>>>
>>> kj::ArrayOutputStream outputStream(arrayPtr);
>>>
>>> ::capnp::writeMessage(outputStream, messageBuilder);
>>>
>>> Resulting error:
>>>
>>> terminate called after throwing an instance of 'kj::ExceptionImpl'
>>> what(): src/kj/io.c++:229: failed: expected size <=
>>> (size_t)(array.end() - fillPos); ArrayOutputStream's backing array was not
>>> large enough for the data written.
>>>
>>> Any help would be appreciated!
>>>
>>> --
>>> 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].
>>> Visit this group at http://groups.google.com/group/capnproto.
>>>
>>
>>
--
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].
Visit this group at https://groups.google.com/group/capnproto.