You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the MessageUnion destructor is called when px_msg goes out of scope it will call delete on a stack-allocated pointer.
There are two workarounds:
Always new any flatbuffers XxxT classes.
Set the MessageUnion type to Message_NONE, which prevents delete being called.
I think the architectural issue is that flatbuffers is assuming any XxxT pointer is originating internally from its own generated functions:
inline SubscribeT *Subscribe::UnPack(const flatbuffers::resolver_function_t *_resolver) const {
auto _o = std::unique_ptr<SubscribeT>(new SubscribeT()); // Side note: this unique_ptr seems redundant.
UnPackTo(_o.get(), _resolver);
return _o.release();
}
But it is nice to be able to use XxxT that you create in all sorts of ways -- including on the stack -- and that don't have their ownership "taken over" when they are put into a union type.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
In this example the root is
PxMsg, which contains a unionMessage.The union implementation is in a
MessageUnionclass generated by flatc. Look at the destructor:Now imagine we have a
PxMsgTthat we create like this:When the
MessageUniondestructor is called whenpx_msggoes out of scope it will calldeleteon a stack-allocated pointer.There are two workarounds:
newany flatbuffersXxxTclasses.MessageUniontype toMessage_NONE, which preventsdeletebeing called.I think the architectural issue is that flatbuffers is assuming any
XxxTpointer is originating internally from its own generated functions:But it is nice to be able to use
XxxTthat you create in all sorts of ways -- including on the stack -- and that don't have their ownership "taken over" when they are put into a union type.Beta Was this translation helpful? Give feedback.
All reactions