Comment 3 for bug 1024033

Revision history for this message
Chris Hillery (ceejatec) wrote :

Nicolae - this is happening because of the following:

    if (result->isStreamable())
    {
      state->theFragmentStream.theStream = &result->getStream();
    }

(lines 195-198 of parse_fragment_impl.cpp). This code assigns the istream to the state, but memory ownership of the stream is still associated with the item (result). Later, this stream is passed to Store.loadDocument() at line 230. So when that item goes out of scope, the stream is freed, and the store is left pointing to a freed istream.

What needs to happen is that state->theFragmentStream needs to have a StreamReleaser field as well. Then, when you assign the value of result->getStream() to theFragmentStream.theStream, you need to also assign the value of result->getStreamReleaser() to theFragmentStream.theStreamReleaser. You also need to call result->setStreamReleaser(nullptr) to complete the transference of memory ownership to theFragmentStream. Finally, in ~FragmentIStream() (or, apparently, actually in FragmentIStream::reset()), if theStreamReleaser is non-null then call it, passing theStream as an argument.

It's not super-clear, but this is how streaming in Zorba works, for better or for worse...