Okay, it seems that before calling "SerializeWithCachedSizes", I need to 
call "ByteSize()", according to 
"https://github.com/protocolbuffers/protobuf/issues/5026";, which fixes this 
problem.

On Thursday, February 20, 2020 at 1:22:03 AM UTC-6, Zi Wang wrote:
>
> Hi,
>
> I have been using protobuf for a while and it worked properly. Recently I 
> have written a new project but has this serialization issue. Here is the 
> example:
>
> In the .proto file, I have this definition:
> ------------
> syntax = "proto2";
>
> package diff_input;
>
> message Diff{
>     message File{
>         required bool val = 1;
>     }
>     required File file = 1;
> }
> ------------
>
> After compiling it to .cc and .h files, I have this simple test:
> -------------------------
> using namespace std;
> using namespace google::protobuf::io;
> using namespace diff_input;
>
> int main(){
>     Diff* file = new Diff();
>     Diff_File* input = new Diff_File();
>     *input->set_val(true);*
>     file->set_allocated_file(input);
>     int outfd = open("serial_file", O_RDWR | O_CREAT|O_TRUNC , 0666);
>     if(outfd == -1){
>         std::cout<<"Open Error\n";
>     }
>     ZeroCopyOutputStream* output = new FileOutputStream(outfd);
>     CodedOutputStream *p_stream = new CodedOutputStream(output);
>     file->SerializeWithCachedSizes(p_stream);
>     delete(p_stream);
>     delete(output);        
>     close(outfd);
>     return 1;
> }
> -------------------------
>
> When I run the executable, I received an error 
> ----------------------
> [libprotobuf FATAL 
> ~/build/external.protobuf/src/external.protobuf/src/google/protobuf/wire_format_lite.cc:580]
>  
> CHECK failed: (end - target) == (size): 
> Aborted
> ----------------------
>
> However, if I commented " *input->set_val(true);*", I would not receive 
> any errors.
>
> My protoc executable is of version "libprotoc 3.5.1".
>
> Many thanks!
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/protobuf/2777093b-a9c6-4462-98e5-bc1ae22de7f1%40googlegroups.com.

Reply via email to