Protobuf:set_allocated_*会删除分配的对象吗?

Protobuf: Will set_allocated_* delete the allocated object?

我有这个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 消息,还是 protobuf 会帮我删除它?我在文档中找不到任何内容。

自问这个问题以来,我一直在寻找答案。也许有人也对答案感兴趣。

从这里开始: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 删除您的对象。