I have this small protobuf code (simplified, only the necessary is contained):
message ParamsMessage {
required int32 temperature = 1;
}
message MasterMessage {
enum Type { GETPARAMS = 1; SENDPARAMS = 2;}
required Type type = 1;
optional ParamsMessage paramsMessage = 2;
}
I now create a MasterMessage in the following way:
ParamsMessage * params = new ParamsMessage();
params->set_temperature(22);
MasterMessage master;
master.set_type(MasterMessage::SENDPARAMS);
master.set_allocated_paramsmessage(params);
The question is: Do I have to (after dealing with the message) delete the params
Message, or will protobuf delete it for me? I cannot find anything in the docs.
As you can see in the charts above, the results for the compressed environment were quite similar for both Protobuf and JSON. Protobuf messages were 9% smaller than JSON messages and they took only 4% less time to be available to the JavaScript code.
Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.
An example schema protobuf represents data as messages whose fields are indicated and aliased with a number and tag. Fields can be required, optional, or repeated. The following message describes a dog. The name is represented as a string, and the field is indicated with the number 1.
Protocol buffer representations of descriptors. This file defines a set of protocol message classes which represent the same information represented by the classes defined in descriptor. h.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With