Search code examples
c++c++11serializationflatbuffers

buffer size every increasing


I'm using flatbuffers to process object streamings, should I call FlatBufferBuilder::Clear each time I finished to created an object? My code is like:

FlatBufferBuild fbb;
while (true) {
    Foo foo;
    RecvFooFromNetwork(&foo);
    FooObject = CreateFooObjectDirect(fbb, foo.x, foo.y, ...);
    SaveToDisk(fbb.GetBufferPointer(), fbb.GetSize());

    // <-- Should I call fbb.Clear() here?
}

I'm observing that fbb.GetSize() is for every increasing even though the size of Foo should be constant. Is that normal?


Solution

  • Yes, you answered your own question, you need to either call Clear() (most efficient) or move the declaration of fbb inside the loop. When you finish a buffer it stays in the builder. There could probably be an assert for creating more data after you call Finish(), maybe open an issue on github?