@@ -34,13 +34,7 @@ DEFINE_bool(
34
34
namespace paddle {
35
35
namespace framework {
36
36
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 (); }
44
38
45
39
Scope& Scope::NewScope () const {
46
40
std::unique_lock<std::mutex> lock (mutex_);
@@ -53,8 +47,9 @@ Variable* Scope::Var(const std::string& name) {
53
47
std::unique_lock<std::mutex> lock (mutex_);
54
48
auto * v = FindVarLocally (name);
55
49
if (v != nullptr ) return v;
50
+
56
51
v = new Variable ();
57
- vars_[name] = v ;
52
+ vars_[name]. reset (v) ;
58
53
VLOG (3 ) << " Create variable " << name;
59
54
v->name_ = &(vars_.find (name)->first );
60
55
return v;
@@ -84,7 +79,7 @@ Variable* Scope::FindVarInternal(const std::string& name) const {
84
79
85
80
const Scope* Scope::FindScope (const Variable* var) const {
86
81
for (auto & kv : vars_) {
87
- if (kv.second == var) {
82
+ if (kv.second . get () == var) {
88
83
return this ;
89
84
}
90
85
}
@@ -123,7 +118,6 @@ void Scope::EraseVars(const std::vector<std::string>& var_names) {
123
118
std::set<std::string> var_set (var_names.begin (), var_names.end ());
124
119
for (auto it = vars_.begin (); it != vars_.end ();) {
125
120
if (var_set.find (it->first ) != var_set.end ()) {
126
- delete it->second ;
127
121
it = vars_.erase (it);
128
122
} else {
129
123
++it;
@@ -139,7 +133,7 @@ void Scope::Rename(const std::string& origin_name,
139
133
auto new_it = vars_.find (new_name);
140
134
PADDLE_ENFORCE (new_it == vars_.end (),
141
135
" The variable with name %s is already in the scope" , new_name);
142
- vars_[new_name] = origin_it->second ;
136
+ vars_[new_name]. reset ( origin_it->second . release ()) ;
143
137
vars_.erase (origin_it);
144
138
}
145
139
@@ -151,7 +145,7 @@ std::string Scope::Rename(const std::string& origin_name) const {
151
145
152
146
Variable* Scope::FindVarLocally (const std::string& name) const {
153
147
auto it = vars_.find (name);
154
- if (it != vars_.end ()) return it->second ;
148
+ if (it != vars_.end ()) return it->second . get () ;
155
149
return nullptr ;
156
150
}
157
151
0 commit comments