尝试从FlatBuffer的二进制文件访问“ LengthofTable”时发生SystemAccessOutOfbound异常

时间:2019-01-30 17:35:22

标签: c# c++ flatbuffers

我正在按照以下架构存储数据。

namespace iCalendarParser;

table EventParsed {

UID:string;
Organizer:string;
Summary:string;
Description:string;
Attendee:[string];
Categories:string;
DtEnd:string;
DtStart:string;
DtStamp:string;
AttendeeCounter:short;

}

table TableofEvents{

  events:[EventParsed];

}

root_type TableofEvents;

我将这样的数据存储在我的c ++应用程序中。

   std::vector<flatbuffers::Offset<flatbuffers::String>> ListOfAttendee;
   std::vector<flatbuffers::Offset<iCalendarParser::EventParsed>> ListofEvents;



    for (auto EventList : ListofAllEvents)
    {

auto Organizer = builder.CreateString(EventList->Organizer);
auto UID = builder.CreateString(EventList->UID);
auto DtStart = builder.CreateString(EventList->DtStart);
auto DtEnd = builder.CreateString(EventList->DtEnd);
auto DtStamp = builder.CreateString(EventList->DtStamp);
auto Summary = builder.CreateString(EventList->Summary);
auto Description = builder.CreateString(EventList->Description);
auto Categories = builder.CreateString(EventList->Categories);

for (int i = 0; i < EventList->AttendeeCounter; i++)
{
    ListOfAttendee.push_back(builder.CreateString(EventList->Attendee[i]));

}


auto VectorListofAttendee = builder.CreateVector(ListOfAttendee);

auto CreatEventInFlatBuffer = CreateEventParsed(builder, UID, Organizer, Summary, Description, VectorListofAttendee, Categories, DtEnd, DtStart, DtStamp, EventList->AttendeeCounter);

ListofEvents.push_back(CreatEventInFlatBuffer);

 }

 auto CreatVectorOfEventInFlatbuffer = builder.CreateVector(ListofEvents);
 auto InsertEventInFlatBufferList = CreateTableofEvents(builder, 
 CreatVectorOfEventInFlatbuffer);
 builder.Finish(InsertEventInFlatBufferList);

 uint8_t *buf = builder.GetBufferPointer();
 int size = builder.GetSize();

 std::ofstream ofile("icalBin.bin", std::ios::binary);
 ofile.write((char *)&buf, size);
 ofile.close();

我正在从C#应用程序尝试访问反序列化数据。

byte[] ReadbytesfromFile = File.ReadAllBytes("icalBin.bin");
var buffer = new ByteBuffer(ReadbytesfromFile);
var tableofEvents = TableofEvents.GetRootAsTableofEvents(buffer); 
int len = tableofEvents.EventsLength;

我不知道出什么问题了。谁能告诉我如何根据架构访问嵌套表并反序列化为C#。

总体思路:

1。获取事件列表。

2。访问单个事件结构。 (我想要得到的结果)

1 个答案:

答案 0 :(得分:1)

我在您的代码中看到2个问题:

  • 它缺少ListOfAttendee.clear(),您的嵌套循环导致此列表在每次迭代中都变得更大。
  • 您正在将指针数据写入文件,而不是指针指向的文件(删除&)。