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