Search code examples
c++flatbuffersdangling-pointer

flatbuffers: Is it possible to delete a buffer via root-pointer


I'm starting to use the flatbuffer library. But there is a thing that feels uncomfortable for me, when it comes to delete a buffer. I don't know ho to delete buffer via it's root-pointer-element. If this were possible the root-pointer could take the ownership of the allocated space and i've not to bother about lifetime-issues(dangling root-pointer, memory-leaks,...)

std::uint_8* buffer = get_buffer_for_exaple_from_file("my_monster.bin");
auto monster = MyGame::Sample::GetMonster(buffer);
///monster points somewhere in buffer

...

///now this would be very handy
delete_buffer(monster);                 ///buffer is deleted

Q: Is it possible (maybe under some restrictions as monster is not mutable, etc) write something like delete_buffer


Solution

  • The root pointer points to a location inside the buffer that is not at a known offset, so no, you can't delete the buffer through it. To delete the buffer, you must use the original buffer pointer.