I'm having a problem writing multiple messages to a file and reading
them back in c++ using GPB 2.3.0.  What I'm trying to do:

I have a GPB generated object that contains a repeated field, e.g.
myobject.proto is:

message MyObject {
        required uint64         x =1;
        required uint32         y =2;
        repeated uint32         z =3 [packed = true];
}

I am taking the object, populating it with some random data and
writing it to a file with the name "filename".  E.g. for method
"populateMyObject" I have:

void populateMyObject(const char * filename){
        google::protobuf::uint64 x;
        google::protobuf::uint32 y;
        google::protobuf::uint32 z;

        //open file for writing
        int fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, S_IREAD |
S_IWRITE);

        //create codedoutputstream
        FileOutputStream output(fd);
        CodedOutputStream codedOutput(&output);

        //create 5 objects with dummy data
        for (int i = 0; i < 5; i++) {
                boost::shared_ptr<MyObject> myObject =
boost::shared_ptr<MyObject>(new MyObject());
                x = i;
                y = (i + 1);
                myObject->set_x(x);
                myObject->set_y(y);

                //populate repeated field with 10 values
                for (ofs::UInt32 idx = 0; idx < 10; idx++) {
                        z = idx;
                        myObject->add_z(z);
                }
                google::protobuf::uint32 myObjectSize = myObject->ByteSize();
                //write size of this serialized object
                codedOutput.WriteVarint32(sparseImageSize);
                //serialize this object to file
                myObject->SerializeToCodedStream(&codedOutput)) {
        }
        output.Flush();
        output.Close();
        close(fd);
}

However, when I run this method, I get the following error:

"libprotobuf FATAL google/protobuf/io/zero_copy_stream_impl_lite.cc:
346] CHECK failed: (buffer_used_) == (buffer_size_):  BackUp() can
only be called after Next()."

Any ideas on what I'm doing wrong here?  The problem is that the fatal
error exits, so I can't continue to any other code, even though it
appears (and I've read the file created successfully) that the file is
created OK with multiple MyObjects being serialized.  Should I be
using some buffer of some kind?  If any one can supply any sample code
that does this successfully I'd be very grateful, thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to