加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – Protobuf:将set_allocated_ *删除分配的对象?

发布时间:2020-12-16 06:02:48 所属栏目:百科 来源:网络整理
导读:我有这个小的protobuf代码(非常简化,只有必需的包含): message ParamsMessage { required int32 temperature = 1;}message MasterMessage { enum Type { GETPARAMS = 1; SENDPARAMS = 2;} required Type type = 1; optional ParamsMessage paramsMessage =
我有这个小的protobuf代码(非常简化,只有必需的包含):
message ParamsMessage {
    required int32 temperature = 1;
}

message MasterMessage {
    enum Type { GETPARAMS = 1; SENDPARAMS = 2;}
    required Type type = 1;

    optional ParamsMessage paramsMessage = 2;

}

我现在以下列方式创建MasterMessage:

ParamsMessage * params = new ParamsMessage();
params->set_temperature(22);
MasterMessage master;
master.set_type(MasterMessage::SENDPARAMS);
master.set_allocated_paramsmessage(params);

问题是:在处理消息后,我有没有删除params消息,或者将原型交易给我?我在文档中找不到任何东西.

解决方法

自问问题以来我一直在找答案.也许有人对答案感兴趣.

从这里:https://developers.google.com/protocol-buffers/docs/reference/cpp-generated

void set_allocated_foo(string* value): Sets the string object to the
field and frees the previous field value if it exists. If the string
pointer is not NULL,the message takes ownership of the allocated
string object and has_foo() will return true. Otherwise,if the value
is NULL,the behavior is the same as calling clear_foo(). string*

release_foo(): Releases the ownership of the field and returns the
pointer of the string object. After calling this,caller takes the
ownership of the allocated string object,has_foo() will return false,
and foo() will return the default value.

这意味着:只要你不调用release_ *,protobuf将会处理删除对象.如果在处理Protobuf消息后需要对象,则需要使用release_ *进行相关处理,这将阻止Protobuf删除对象.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读