lundi 14 juin 2021

Return pointer to flatbuffer from a method

I have a class like this, is it valid to return flatbuffer pointer even though builder is created on stack

class Foo {
   uint8_t*  serialize() {
     flatbuffers::FlatBufferBuilder builder(1024);
     ....
     ....
     return builder.GetBufferPointer();
   }
}

From documentation here https://google.github.io/flatbuffers/md__cpp_usage.html

"Calling code may take ownership of the buffer with fbb.ReleaseBufferPointer(). Should you do it, the FlatBufferBuilder will be in an invalid state, and must be cleared before it can be used again. However, it also means you are able to destroy the builder while keeping the buffer in your application."

Look like this should work and returned pointer will point to valid buffer even if builder is destroyed. Can someone confirm this, looks like buffer itself is created on heap.

Aucun commentaire:

Enregistrer un commentaire