Protobuf: Will set_allocated_* delete the allocated object?

后端 未结 1 861
谎友^
谎友^ 2020-12-14 14:36

I have this small protobuf code (simplified, only the necessary is contained):

message ParamsMessage {
    required int32 temperature = 1;
}

message MasterM         


        
相关标签:
1条回答
  • 2020-12-14 15:24

    Since asking the question I have continued to find the answer. Maybe someone is interested in the answer, too.

    From here: 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.

    Which means: As long as you do not call release_*, protobuf will take care of deleting the object. If you need the Object after dealing with the Protobuf Message, you need to relase it using release_*, which will prevent Protobuf to delete your object.

    0 讨论(0)
提交回复
热议问题