0

I read compressed data from disk, uncompress it, and create a "Cell" object that was generated from a FlatBuffer scheme.

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

So far so good. But how can I delete the item? Since it's not possible to access the buffer data later on.

benjist
  • 2,740
  • 3
  • 31
  • 58
  • Not enough information to give a sane answer. Typically the right place would be the `Cell` destructor, if `Cell` holds onto it. If not, `Cell * temp = GetCell(buffer); delete buffer; return temp;` – user4581301 Oct 15 '16 at 17:32
  • Sorry, but you have no idea what you talk about. The Cell class is generated code. The Cell object is created from an endian aligned offset into the buffer data. Somebody who knows FlatBuffer will also know what I talk about... In other words: I have no real control about how the destructor is implemented (it's generated), and I do not have access to the raw buffer again. I could wrap it into an own object - but then again I think it's a common usage pattern and FlatBuffer offers something on it's own for this scenario. – benjist Oct 15 '16 at 17:38

1 Answers1

1

You'll need to hold on to the buffer separately to be able to delete it, as you cannot derive a buffer pointer from a root pointer (in this case Cell *). As you say yourself, you cannot destruct a Cell * either, as it doesn't own that memory.

Edit: apparently, such a function is possible, see: https://github.com/google/flatbuffers/commit/6862b2ff08021c7ba474334a6e2a3f3b1fc0dee5 This derives a buffer pointer from a root pointer.

Aardappel
  • 5,559
  • 1
  • 19
  • 22