@@ -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_);
@@ -51,8 +45,9 @@ Scope& Scope::NewScope() const {
51
45
Variable* Scope::Var (const std::string& name) {
52
46
auto * v = FindVarLocally (name);
53
47
if (v != nullptr ) return v;
48
+
54
49
v = new Variable ();
55
- vars_[name] = v ;
50
+ vars_[name]. reset (v) ;
56
51
VLOG (3 ) << " Create variable " << name;
57
52
v->name_ = &(vars_.find (name)->first );
58
53
return v;
@@ -76,7 +71,7 @@ Variable* Scope::FindVar(const std::string& name) const {
76
71
77
72
const Scope* Scope::FindScope (const Variable* var) const {
78
73
for (auto & kv : vars_) {
79
- if (kv.second == var) {
74
+ if (kv.second . get () == var) {
80
75
return this ;
81
76
}
82
77
}
@@ -113,7 +108,6 @@ void Scope::EraseVars(const std::vector<std::string>& var_names) {
113
108
std::set<std::string> var_set (var_names.begin (), var_names.end ());
114
109
for (auto it = vars_.begin (); it != vars_.end ();) {
115
110
if (var_set.find (it->first ) != var_set.end ()) {
116
- delete it->second ;
117
111
it = vars_.erase (it);
118
112
} else {
119
113
++it;
@@ -129,7 +123,7 @@ void Scope::Rename(const std::string& origin_name,
129
123
auto new_it = vars_.find (new_name);
130
124
PADDLE_ENFORCE (new_it == vars_.end (),
131
125
" 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 ()) ;
133
127
vars_.erase (origin_it);
134
128
}
135
129
@@ -141,7 +135,7 @@ std::string Scope::Rename(const std::string& origin_name) const {
141
135
142
136
Variable* Scope::FindVarLocally (const std::string& name) const {
143
137
auto it = vars_.find (name);
144
- if (it != vars_.end ()) return it->second ;
138
+ if (it != vars_.end ()) return it->second . get () ;
145
139
return nullptr ;
146
140
}
147
141
0 commit comments