protobuf stringstream codedoutputstream issues

603 views
Skip to first unread message

Predator33

unread,
Apr 14, 2013, 2:26:57 PM4/14/13
to prot...@googlegroups.com
Got a bit of confusion with using stringstream and the CodedOutputStream of protobuf. Essentially, for each packet i put in a packetid that is stored inside a protobuf message, then i put in the actual contents which is also a protobuf message.

This code used to work when i was creating an std::stringstream just like you see below, and passing a pointer to it into this method and having it use that. Now that I removed that functionality, for some reason the stream is just entirely empty. The only thing I could think of is maybe the protobuf stream doesn't flush itself out until the destructor is hit and since that never happens until after the fact, it never gets flushed?

That was the only theory I could grab. Some tips would be great.

std::string serialize(google::protobuf::Message* message, uint32_t packetType)
{
    std::stringstream ss(std::stringstream::out | std::stringstream::binary);

    google::protobuf::io::OstreamOutputStream raw_out(&ss);
    google::protobuf::io::CodedOutputStream coded_out(&raw_out);

    std::string headerString;

    // write packet header, containing type of message we're sending
    PacketBuf::Packet p;
    p.set_type(packetType);
    p.SerializeToString(&headerString);

    coded_out.WriteVarint32(headerString.size());
    coded_out.WriteRaw(headerString.data(), headerString.size());

    std::string contentsString;
    // write actual contents
    message->SerializeToString(&contentsString);

    coded_out.WriteVarint32(contentsString.size());
    coded_out.WriteString(contentsString);

    assert(ss.str().size() > 0); // assertion is triggered :-(

    return ss.str();
}

Oliver Jowett

unread,
Apr 14, 2013, 4:59:26 PM4/14/13
to Predator33, Protocol Buffers
On Sun, Apr 14, 2013 at 7:26 PM, Predator33 <preda...@gmail.com> wrote:
The only thing I could think of is maybe the protobuf stream doesn't flush itself out until the destructor is hit and since that never happens until after the fact, it never gets flushed?

Yes, this will be what is happening. Arrange for the OstreamOutputStream to be destroyed before you try to use its output (e.g. wrap that declaration in another level of nesting)

Is there some reason why you're using stringstream + OstreamOutputStream, rather than just using StringOutputStream?

Oliver

Rinka Yoshida

unread,
Jul 29, 2019, 4:42:49 PM7/29/19
to Protocol Buffers
I'm experiencing the same issue too and I was wondering if you could please elaborate on what you mean by destroying it before using its output? Delete OstreamOutputStream and try to get the string contained in stringstream?

I tried using StringOutputStream, but it seems like when I did coded_output->WriteString(serialized_string), sometimes serialized_string's content will be chopped off. I was wondering if you also might have any idea why this is happening.

Adam Cozzette

unread,
Jul 30, 2019, 7:35:25 PM7/30/19
to Rinka Yoshida, Protocol Buffers
You can use an extra layer of curly-brace nesting to ensure the CodedOutputStream is destroyed before you examine the output. See this test code for example. I think if you let the CodedOutputStream be destroyed first then that will fix the problem with your StringOutputStream code.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/dfc6c277-349f-465a-81d9-f87c8de84985%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages