Comment #9 on issue 402 by
liuj...@google.com: question for clear and parse
http://code.google.com/p/protobuf/issues/detail?id=402
If we have such a message below, where Bar is a message type.
message Foo {
optional Bar bar = 1;
}
Then in C++ generated class, Foo class will have a Bar pointer for the
field, (Bar* bar;}
1) Before the first ParseFromCodedInputStream(), the bar pointer will be
NULL.
2) In the parsing function, we will new a Bar() instance and assign it to
the bar_ pointer.
3) After the foo.Clear(), we call bar.Clear() in the function, and bar will
still point to the message we allocated in the step 2.
4) The next ParseFromCodedInputStream(), we will directly merge the bytes
into the bar pointer, instead of creating a new one.
5) The allocated bar message will be deleted when the parent foo message is
destroyed.
That said, there will no memory leaks, but it's possible that you could
observe increasing memory usages in the repeated Clear() and ParseFrom().
(Imagine you have thousands of message fields, and each time there is some
message allocated) But those messages are just cached for the next
MergeFrom() to reduce the allocating cost and memory footprint. They will
be deleted once the parent message is deleted. If you find the memory usage
is increasing linearly and boundlessly, then there must be something wrong
in your code.