根据我看到的奇怪行为,我猜测以下代码无效。我的问题是:下面创建的 msgpack_object 是否依赖于 msgpack_sbuffer?也就是说,一旦调用 msgpack_sbuffer_free(buffer),msgpack_object(在 msg.data 中)是否无效?如果是这样,在这种情况下获得没有依赖关系的堆分配 msgpack_object 的正确方法是什么?
msgpack_object create_static_msg_object() {
msgpack_sbuffer* buffer = msgpack_sbuffer_new();
msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
// does some calls to msgpack_pack_*() here
msgpack_unpacked msg;
msgpack_unpacked_init(&msg);
msgpack_unpack_next(&msg, buffer->data, buffer->size, NULL);
/* cleaning */
msgpack_sbuffer_free(buffer);
msgpack_packer_free(pk);
return msg.data;
}