如何正确删除从磁盘读取的 FlatBuffer 对象

How to properly delete a FlatBuffer object read from disk

我从磁盘读取压缩数据,将其解压缩,然后创建一个 "Cell" 从 FlatBuffer 方案生成的对象。

Cell* getCell(int x, int y, int z) {
     // ...
    return GetCell(buffer); // buffer is an inflated uint_8 data array 
}

到目前为止一切顺利。但是我怎样才能删除该项目?由于以后无法访问缓冲区数据。

您需要单独保留缓冲区才能将其删除,因为您无法从根指针派生缓冲区指针(在本例中为 Cell *)。正如您自己所说,您也不能破坏 Cell *,因为它不拥有该内存。

编辑:显然,这样的功能是可能的,参见:https://github.com/google/flatbuffers/commit/6862b2ff08021c7ba474334a6e2a3f3b1fc0dee5 这从根指针导出缓冲区指针。