提交 8938a9b0 编写于 作者: Y Yu Yang 提交者: GitHub

Correct implement BlockDesc destructor (#4882)

上级 521514da
...@@ -72,13 +72,13 @@ std::vector<OpDescBind *> BlockDescBind::AllOps() const { ...@@ -72,13 +72,13 @@ std::vector<OpDescBind *> BlockDescBind::AllOps() const {
void BlockDescBind::Flush() { void BlockDescBind::Flush() {
if (need_update_) { if (need_update_) {
auto &op_field = *this->desc_->mutable_ops(); auto &op_field = *this->desc_->mutable_ops();
op_field.Clear(); this->ClearPBOps();
op_field.Reserve(static_cast<int>(ops_.size())); op_field.Reserve(static_cast<int>(ops_.size()));
for (auto &op_desc : ops_) { for (auto &op_desc : ops_) {
op_field.AddAllocated(op_desc->Proto()); op_field.AddAllocated(op_desc->Proto());
} }
auto &var_field = *this->desc_->mutable_vars(); auto &var_field = *this->desc_->mutable_vars();
var_field.Clear(); this->ClearPBVars();
var_field.Reserve(static_cast<int>(vars_.size())); var_field.Reserve(static_cast<int>(vars_.size()));
for (auto &var_desc : vars_) { for (auto &var_desc : vars_) {
var_field.AddAllocated(var_desc.second->Proto()); var_field.AddAllocated(var_desc.second->Proto());
...@@ -99,5 +99,21 @@ BlockDesc *BlockDescBind::Proto() { ...@@ -99,5 +99,21 @@ BlockDesc *BlockDescBind::Proto() {
return desc_; return desc_;
} }
void BlockDescBind::ClearPBOps() {
auto ops = this->desc_->mutable_ops();
while (!ops->empty()) {
// we do not own the OpDesc, so release the ownership.
ops->ReleaseLast();
}
}
void BlockDescBind::ClearPBVars() {
auto vars = this->desc_->mutable_vars();
while (!vars->empty()) {
// we do not own the VarDesc, so release the ownership.
vars->ReleaseLast();
}
}
} // namespace framework } // namespace framework
} // namespace paddle } // namespace paddle
...@@ -36,6 +36,11 @@ class BlockDescBind { ...@@ -36,6 +36,11 @@ class BlockDescBind {
BlockDescBind(ProgramDescBind *prog, BlockDesc *desc) BlockDescBind(ProgramDescBind *prog, BlockDesc *desc)
: prog_(prog), desc_(desc), need_update_(false) {} : prog_(prog), desc_(desc), need_update_(false) {}
~BlockDescBind() {
this->ClearPBVars();
this->ClearPBOps();
}
int32_t ID() const { return desc_->idx(); } int32_t ID() const { return desc_->idx(); }
int32_t Parent() const { return desc_->parent_idx(); } int32_t Parent() const { return desc_->parent_idx(); }
...@@ -60,6 +65,10 @@ class BlockDescBind { ...@@ -60,6 +65,10 @@ class BlockDescBind {
BlockDesc *Proto(); BlockDesc *Proto();
private:
void ClearPBOps();
void ClearPBVars();
// FIXME(yuyang18): backward will access private data of BlockDesc. // FIXME(yuyang18): backward will access private data of BlockDesc.
// Mark it public temporary. We can fix it later. // Mark it public temporary. We can fix it later.
public: public:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册