Home > Enterprise >  C FlatBuffers - Assertion failed: (finished), function Finished
C FlatBuffers - Assertion failed: (finished), function Finished

Time:03-06

Here is my schema:

namespace Vibranium;

enum GameObject_Type:uint { STATIC = 0 }

table GameObjectDatabase
{
    gameobjects:[GameObjectsTemplate];
}

table GameObjectsTemplate
{
    id:int;
    name:string;
    prefab:string;
    type:GameObject_Type;
    position_x:float;
    position_y:float;
    position_z:float;
    rotation_x:float;
    rotation_y:float;
    rotation_z:float;
    map_id:int;
    event_id:int;
}

root_type GameObjectDatabase;

Here is my code:

flatbuffers::FlatBufferBuilder fbb;

std::vector<flatbuffers::Offset<Vibranium::GameObjectsTemplate>> gameobjects_template;

Field* fields = result->Fetch();
auto id = fields[0].GetInt32();
auto name = fbb.CreateString(fields[1].GetString());
auto prefab = fbb.CreateString(fields[2].GetString());
auto type = static_cast<Vibranium::GameObject_Type>(fields[3].GetInt32());
auto position_x = fields[5].GetFloat();
auto position_y = fields[6].GetFloat();
auto position_z = fields[7].GetFloat();
auto rotation_x = fields[8].GetFloat();
auto rotation_y = fields[9].GetFloat();
auto rotation_z = fields[10].GetFloat();
auto map_id = fields[11].GetInt32();
auto event_id = fields[12].GetInt32();

auto go1 = Vibranium::CreateGameObjectsTemplate(fbb, id, name, prefab, type,position_x,position_y,position_z, rotation_x,rotation_y,rotation_z,map_id,event_id);
gameobjects_template.push_back(go1);
this->_container.push_back(go1);
auto go_db = fbb.CreateVector(gameobjects_template);

Vibranium::GameObjectDatabaseBuilder go_builder(fbb);
go_builder.add_gameobjects(go_db);
go_builder.Finish();

auto gt = Vibranium::GetGameObjectDatabase(fbb.GetBufferPointer());

I get this error:

Assertion failed: (finished), function Finished, file /opt/homebrew/include/flatbuffers/flatbuffers.h, line 1226.

It is caused by this line:

auto gt = Vibranium::GetGameObjectDatabase(fbb.GetBufferPointer());

Why is that and how can I fix it?

CodePudding user response:

If you get an assert, it is always good to check the assert code, it may give you a hint as to why:

  void Finished() const {
    // If you get this assert, you're attempting to get access a buffer
    // which hasn't been finished yet. Be sure to call
    // FlatBufferBuilder::Finish with your root table.
    // If you really need to access an unfinished buffer, call
    // GetCurrentBufferPointer instead.
    FLATBUFFERS_ASSERT(finished);
  }
  • Related