Hi Kaushik,
An Array<T> owns its contents. When the Array<T> goes out-of-scope, the
contents are destroyed. Array<T>'s can be passed-by-move to transfer
ownership.
ArrayPtr<T> does *not* own its content -- it is a pointer to some other
array.
So, in your implementation, you will want to declare your vector as:
std::vector<kj::Array<capnp::word>>
And populate it like:
encodedSessionData[i] = kj::mv(flatArray);
(Note that since `.asBytes()` returns ArrayPtr, you'll have to delay
calling it until you are extracting the array from the vector later.)
-Kenton
On Tue, Jun 7, 2016 at 1:50 PM, <[email protected]> wrote:
> 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.
>
--
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.