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
-
In this example the root is
PxMsg
, which contains a unionMessage
.The union implementation is in a
MessageUnion
class generated by flatc. Look at the destructor:Now imagine we have a
PxMsgT
that we create like this:When the
MessageUnion
destructor is called whenpx_msg
goes out of scope it will calldelete
on a stack-allocated pointer.There are two workarounds:
new
any flatbuffersXxxT
classes.MessageUnion
type toMessage_NONE
, which preventsdelete
being called.I think the architectural issue is that flatbuffers is assuming any
XxxT
pointer is originating internally from its own generated functions: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.Beta Was this translation helpful? Give feedback.
All reactions