IndexOutOfBoundsException while de-serialization in JAVA for a flatbuffer written in C++

267 views
Skip to first unread message

Ranjith Chungath

unread,
Jan 15, 2015, 12:57:00 PM1/15/15
to flatb...@googlegroups.com
Hi All,

 The following is my fbs file:

===================================
namespace com.helloworld;

table MyObject {
  field1:string;
  field2:[double];
  field3:[double];
  field4:string;
  field5:string;
  field6:string;
}

table MyObjectList {
  objects:[MyObject];
}

root_type MyObjectList;

===================================
The following is my code to serialize the buffer.
===================================
    
    flatbuffers::FlatBufferBuilder fbb
    std::vector< flatbuffers::Offset<MyObject> > objs;

    std::vector<double> vec1 ;  // Initialized with the values : "1.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.09 1.00"
    std::vector<double> vec2 ;  // Initialized with the values : "1.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 -0.04455 0.049 1.00"
    
    for( int i=0; i<objectCount; i++ )
    {
        // Populate the fields
        flatbuffers::Offset<flatbuffers::String> field1 = fbb.CreateString( "field1" );
        flatbuffers::Offset<flatbuffers::Vector<double>> field2 = fbb.CreateVector( vec1 );
        flatbuffers::Offset<flatbuffers::Vector<double>> field3 = fbb.CreateVector( vec1 );
        flatbuffers::Offset<flatbuffers::String> field4 = fbb.CreateString( "field2" );
        flatbuffers::Offset<flatbuffers::String> field5 = fbb.CreateString( "field2" );
        flatbuffers::Offset<flatbuffers::String> field6 = fbb.CreateString( "field2" );        

        // Create the Object
        MyObjectBuilder  obj( fbb );
        obj.add_field6( field5 );
        obj.add_field5( field5 );
        obj.add_field4( field4 );
        obj.add_field3( field3 );
        obj.add_field2( field2 );
        obj.add_field1( field1 );

        // Finished writing the element
        flatbuffers::Offset<MyObject> objOffset = obj.Finish();
        objs.push_back( objOffset );
    }

    // Create the Offset vector of the elements
    flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<MyObject>>> objOffsets = fbb.CreateVector( objs );

    // Build the MyObjectList 
    MyObjectListBuilder listBuffer( fbb );
    listBuffer.add_objects( objOffsets );
    flatbuffers::Offset<MyObjectList> listOffset = listBuffer.Finish();

    // Finish writing the builder with the MyObjectList as root
    fbb.Finish( listOffset );
===================================
The serialization happens without any errors..

But when I de-serialize the buffer through JAVA, I get an IndexOutOfBounds while accessing the fields on any object.
java.lang.IndexOutOfBoundsException
at java.nio.Buffer.checkIndex(Buffer.java:520)
at java.nio.HeapByteBuffer.getShort(HeapByteBuffer.java:289)
at flatbuffers.Table.__offset(Table.java:34)
at com.helloworld.MyObject.field1(MyObject.java:13)

Also, if the number of objects being serialized is less than 10, the de-serialization happens fine. The moment the number of objects becomes 10 or more, the problem appears.

The Java code to de-serialize is given below
===================================
        MyObjectList list = MyObjectList.getRootAsMyObjectList( bb, 0 );
        
        int size = list.objectsLength();        

        for (int i = 0; i < size; i++) {
            MyObject obj = list.objects( i );
            
            String field1 = obj.field1();
            String field4 = obj.field4();
            String field5 = obj.field5();
            String field6 = obj.field6();
            
            System.out.println(" field1 = " + field1 );
            System.out.println(" field4 = " + field4 );
            System.out.println(" field5 = " + field5 );
            System.out.println(" field6 = " + field6 );
        }
===================================

Can you please let me know what is going wrong here. I am thinking the problem is while serializing in the C++ code, but not able to figure out what exactly I am doing wrong. Is there a problem with the way I am creating and writing the Offset of vectors of objects?

Thanks,
Ranjith

Wouter van Oortmerssen

unread,
Jan 16, 2015, 7:00:20 PM1/16/15
to Ranjith Chungath, flatb...@googlegroups.com
The code so far looks correct, but there may be something going wrong in the transport between the two.

In C++, you are transporting GetSize() bytes starting from GetBufferPointer() ? And those bytes then end up at offset 0 in the ByteBuffer?

--
You received this message because you are subscribed to the Google Groups "FlatBuffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flatbuffers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ranjith Chungath

unread,
Jan 20, 2015, 1:30:09 AM1/20/15
to flatb...@googlegroups.com, ranjith....@gmail.com
Thank you for your confirmation.

The problem turned out to be the way in which I was transferring the files. It was getting transferred as text instead of binary which caused the problem. Once I corrected that, it worked fine.

Ranjith
Reply all
Reply to author
Forward
0 new messages