Schema:
/* Model.fbs
namespace Resource.Model;
struct Vec3 {
x:float;
y:float;
z:float;
}
struct Vec2 {
x:float;
y:float;
}
table MeshData {
Position:[Vec3];
Normal:[Vec3];
TexCoords:[Vec2];
Tangent:[Vec3];
Bitangent:[Vec3];
}
table VertexObject {
MData:MeshData;
Indices:[uint];
MatNameHash:uint;
}
table Model {
Name:uint;
Meshes:[VertexObject];
}
root_type Model;
*/
Building:
//....
auto a = builder.CreateVectorOfStructs(Positions);
auto b = builder.CreateVectorOfStructs(Normals);
auto c = builder.CreateVectorOfStructs(TexCoords);
auto d = builder.CreateVectorOfStructs(Tangents);
auto e = builder.CreateVectorOfStructs(Bitangents);
auto f = CreateMeshData(builder, a, b, c, d, e);
auto g = builder.CreateVector<uint32_t>(Indices);
unsigned int h = 1024; //Hash of Name of Material
std::vector<flatbuffers::Offset<VertexObject>> VO;
auto i = CreateVertexObject(builder, f, g, h);
auto i2 = CreateVertexObject(builder, f, g, h);
VO.push_back(i);
VO.push_back(i2);
auto j = builder.CreateVector(VO);
//...
Loading:
auto test = GetModel(data);
assert(test->Name());
auto name = test->Name();
assert(test->Meshes());
auto FBMeshes = test->Meshes();
for (unsigned int i = 0; i < FBMeshes->size(); i++)
{
assert(FBMeshes->Get(i));
auto FBMeshesIndex = FBMeshes->Get(i);
//assert(FBMeshesIndex->MData());
}
I get an Access Violation when trying to call a Table Vector in Flatbuffers. Included is the Schema, and Source Code. I believe it to be something to do with how the components serialized together for the MeshData Table, but I am unable to figure out where exactly. I get the Error when I try to run assert(FBMeshesIndex->MData()); which is Root->Vec:Tables()->Get(i)->Vec:Tables() of which contains several Vec of Structs.
TestBed.exe!flatbuffers::ReadScalar(const void * p) Line 219 C++ Symbols loaded. TestBed.exe!flatbuffers::Table::GetOptionalFieldOffset(unsigned short field) Line 1866 C++ Symbols loaded. TestBed.exe!flatbuffers::Table::GetPointer(unsigned short field) Line 1878 C++ Symbols loaded. TestBed.exe!flatbuffers::Table::GetPointer(unsigned short field) Line 1885 C++ Symbols loaded. TestBed.exe!Resource::Model::VertexObject::MData() Line 180 C++ Symbols loaded. TestBed.exe!main() Line 147 C++ Symbols loaded. [External Code] Annotated Frame
The code looks ok, so the problem is likely outside of the code shown, in how data
was transported from being built to being used.
Is the value of name
1024? is FBMeshes->size()
2? Additionally, you should run a verifier on data
prior to accessing it, to see if there were any problems with how the data was passed along.