My project uses gRPC
and generates code like this:
......
inline void AddDBResponse::clear_last_record() {
if (has_last_record()) {
delete msg_.last_record_;
clear_has_msg();
}
}
......
inline ::privdb::DB* AddDBResponse::release_last_record() {
// @@protoc_insertion_point(field_release:privdb.AddDBResponse.last_record)
if (has_last_record()) {
clear_has_msg();
::privdb::DB* temp = msg_.last_record_;
msg_.last_record_ = NULL;
return temp;
} else {
return NULL;
}
}
inline void AddDBResponse::set_allocated_last_record(::privdb::DB* last_record) {
clear_msg();
if (last_record) {
set_has_last_record();
msg_.last_record_ = last_record;
}
// @@protoc_insertion_point(field_set_allocated:privdb.AddDBResponse.last_record)
}
......
In clear_last_record()
method, it will assume the msg_.last_record_
is allocated in heap, so free it; while release_last_record()
not. So if you callset_allocated_last_record()
function and last_record
points the data which resides on stack literally, please use release_last_record()
. Otherwise, the scary memory corruption will occur!:-)