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

前端之家收集整理的这篇文章主要介绍了c – Protobuf:将set_allocated_ *删除分配的对象?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个小的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@H_502_18@ field and frees the prevIoUs field value if it exists. If the string@H_502_18@ pointer is not NULL,the message takes ownership of the allocated@H_502_18@ string object and has_foo() will return true. Otherwise,if the value@H_502_18@ is NULL,the behavior is the same as calling clear_foo(). string*

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

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

猜你在找的C&C++相关文章