Skip to content

Commit 106ee9d

Browse files
authored
Merge pull request #11243 from panyx0718/scope
small clean up and document pointer ownership.
2 parents ea408d5 + 73aa5d2 commit 106ee9d

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

paddle/fluid/framework/scope.cc

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,7 @@ DEFINE_bool(
3434
namespace paddle {
3535
namespace framework {
3636

37-
Scope::~Scope() {
38-
DropKids();
39-
for (auto& kv : vars_) {
40-
VLOG(3) << "Destroy variable " << kv.first;
41-
delete kv.second;
42-
}
43-
}
37+
Scope::~Scope() { DropKids(); }
4438

4539
Scope& Scope::NewScope() const {
4640
std::unique_lock<std::mutex> lock(mutex_);
@@ -51,8 +45,9 @@ Scope& Scope::NewScope() const {
5145
Variable* Scope::Var(const std::string& name) {
5246
auto* v = FindVarLocally(name);
5347
if (v != nullptr) return v;
48+
5449
v = new Variable();
55-
vars_[name] = v;
50+
vars_[name].reset(v);
5651
VLOG(3) << "Create variable " << name;
5752
v->name_ = &(vars_.find(name)->first);
5853
return v;
@@ -76,7 +71,7 @@ Variable* Scope::FindVar(const std::string& name) const {
7671

7772
const Scope* Scope::FindScope(const Variable* var) const {
7873
for (auto& kv : vars_) {
79-
if (kv.second == var) {
74+
if (kv.second.get() == var) {
8075
return this;
8176
}
8277
}
@@ -113,7 +108,6 @@ void Scope::EraseVars(const std::vector<std::string>& var_names) {
113108
std::set<std::string> var_set(var_names.begin(), var_names.end());
114109
for (auto it = vars_.begin(); it != vars_.end();) {
115110
if (var_set.find(it->first) != var_set.end()) {
116-
delete it->second;
117111
it = vars_.erase(it);
118112
} else {
119113
++it;
@@ -129,7 +123,7 @@ void Scope::Rename(const std::string& origin_name,
129123
auto new_it = vars_.find(new_name);
130124
PADDLE_ENFORCE(new_it == vars_.end(),
131125
"The variable with name %s is already in the scope", new_name);
132-
vars_[new_name] = origin_it->second;
126+
vars_[new_name].reset(origin_it->second.release());
133127
vars_.erase(origin_it);
134128
}
135129

@@ -141,7 +135,7 @@ std::string Scope::Rename(const std::string& origin_name) const {
141135

142136
Variable* Scope::FindVarLocally(const std::string& name) const {
143137
auto it = vars_.find(name);
144-
if (it != vars_.end()) return it->second;
138+
if (it != vars_.end()) return it->second.get();
145139
return nullptr;
146140
}
147141

paddle/fluid/framework/scope.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,18 @@ class Scope {
4747
Scope& NewScope() const;
4848

4949
/// Create a variable with given name if it doesn't exist.
50+
/// Caller doesn't own the returned Variable.
5051
Variable* Var(const std::string& name);
5152

5253
/// Create a variable with a scope-unique name.
54+
/// Caller doesn't own the returned Variable.
5355
Variable* Var(std::string* name = nullptr);
5456

5557
void EraseVars(const std::vector<std::string>& var_names);
5658

5759
/// Find a variable in the scope or any of its ancestors. Returns
5860
/// nullptr if cannot find.
61+
/// Caller doesn't own the returned Variable.
5962
Variable* FindVar(const std::string& name) const;
6063

6164
const Scope* parent() const { return parent_; }
@@ -78,13 +81,15 @@ class Scope {
7881
// Rename variable to a new name and return the new name
7982
std::string Rename(const std::string& origin_name) const;
8083

84+
/// Caller doesn't own the returned Variable.
8185
Variable* FindVarLocally(const std::string& name) const;
8286

8387
private:
8488
// Call Scope::NewScope for a sub-scope.
8589
explicit Scope(Scope const* parent) : parent_(parent) {}
8690

87-
mutable std::unordered_map<std::string, Variable*> vars_;
91+
mutable std::unordered_map<std::string, std::unique_ptr<Variable>> vars_;
92+
// Scope in `kids_` are owned by this class.
8893
mutable std::list<Scope*> kids_;
8994
Scope const* parent_{nullptr};
9095

0 commit comments

Comments
 (0)