@@ -28,27 +28,20 @@ namespace internal
28
28
// when the slot is disconnected. Bug 167714.
29
29
struct self_and_iter : public notifiable
30
30
{
31
- signal_impl* self_;
31
+ const std::shared_ptr< signal_impl> self_;
32
32
const signal_impl::iterator_type iter_;
33
33
34
- self_and_iter (signal_impl* self, const signal_impl::iterator_type& iter) : self_(self), iter_(iter) {}
34
+ self_and_iter (const std::shared_ptr< signal_impl>& self, const signal_impl::iterator_type& iter) : self_(self), iter_(iter) {}
35
35
};
36
36
37
- signal_impl::signal_impl () : ref_count_( 0 ), exec_count_(0 ), deferred_(false )
37
+ signal_impl::signal_impl () : exec_count_(0 ), deferred_(false )
38
38
{
39
39
}
40
40
41
41
signal_impl::~signal_impl ()
42
42
{
43
- // unreference() must not call ~signal_impl() while clear() is executing.
44
- ++ref_count_;
45
-
46
43
// Disconnect all slots before *this is deleted.
47
44
clear ();
48
-
49
- // Now ref_count_ can be cleared again (not really necessary), but don't do it
50
- // with a call to unreference(). That would invoke ~signal_impl() recursively.
51
- --ref_count_;
52
45
}
53
46
54
47
// only MSVC needs this to guarantee that all new/delete are executed from the DLL module
@@ -72,7 +65,7 @@ signal_impl::clear()
72
65
// Don't let signal_impl::notify() erase the slots. It would invalidate the
73
66
// iterator in the following loop.
74
67
const bool saved_deferred = deferred_;
75
- signal_exec exec (this );
68
+ signal_exec exec (shared_from_this () );
76
69
77
70
// Disconnect all connected slots before they are deleted.
78
71
// signal_impl::notify() will be called and delete the self_and_iter structs.
@@ -128,7 +121,7 @@ signal_impl::erase(iterator_type i)
128
121
// Don't let signal_impl::notify() erase the slot. It would be more
129
122
// difficult to get the correct return value from signal_impl::erase().
130
123
const bool saved_deferred = deferred_;
131
- signal_exec exec (this );
124
+ signal_exec exec (shared_from_this () );
132
125
133
126
// Disconnect the slot before it is deleted.
134
127
// signal_impl::notify() will be called and delete the self_and_iter struct.
@@ -142,7 +135,7 @@ signal_impl::erase(iterator_type i)
142
135
void
143
136
signal_impl::add_notification_to_iter (const signal_impl::iterator_type& iter)
144
137
{
145
- auto si = new self_and_iter (this , iter);
138
+ auto si = new self_and_iter (shared_from_this () , iter);
146
139
iter->set_parent (si, &signal_impl::notify_self_and_iter_of_invalidated_slot);
147
140
}
148
141
@@ -168,7 +161,7 @@ signal_impl::sweep()
168
161
// The deletion of a slot may cause the deletion of a signal_base,
169
162
// a decrementation of ref_count_, and the deletion of this.
170
163
// In that case, the deletion of this is deferred to ~signal_exec().
171
- signal_exec exec (this );
164
+ signal_exec exec (shared_from_this () );
172
165
173
166
deferred_ = false ;
174
167
auto i = slots_.begin ();
@@ -207,13 +200,12 @@ signal_impl::notify_self_and_iter_of_invalidated_slot(notifiable* d)
207
200
208
201
} /* namespace internal */
209
202
210
- signal_base::signal_base () noexcept : impl_( nullptr )
203
+ signal_base::signal_base () noexcept
211
204
{
212
205
}
213
206
214
207
signal_base::signal_base (const signal_base& src) noexcept : trackable(), impl_(src.impl())
215
208
{
216
- impl_->reference ();
217
209
}
218
210
219
211
signal_base::signal_base (signal_base&& src) : trackable(std::move(src)), impl_(std::move(src.impl_))
@@ -223,10 +215,6 @@ signal_base::signal_base(signal_base&& src) : trackable(std::move(src)), impl_(s
223
215
224
216
signal_base::~signal_base ()
225
217
{
226
- if (impl_)
227
- {
228
- impl_->unreference ();
229
- }
230
218
}
231
219
232
220
void
@@ -298,13 +286,7 @@ signal_base::operator=(const signal_base& src)
298
286
if (src.impl_ == impl_)
299
287
return *this ;
300
288
301
- if (impl_)
302
- {
303
- impl_->unreference ();
304
- }
305
-
306
289
impl_ = src.impl ();
307
- impl_->reference ();
308
290
return *this ;
309
291
}
310
292
@@ -314,25 +296,19 @@ signal_base::operator=(signal_base&& src)
314
296
if (src.impl_ == impl_)
315
297
return *this ;
316
298
317
- if (impl_)
318
- {
319
- impl_->unreference ();
320
- }
321
-
322
299
src.notify_callbacks ();
323
300
impl_ = src.impl_ ;
324
301
src.impl_ = nullptr ;
325
302
326
303
return *this ;
327
304
}
328
305
329
- internal::signal_impl*
306
+ std::shared_ptr< internal::signal_impl>
330
307
signal_base::impl () const
331
308
{
332
309
if (!impl_)
333
310
{
334
- impl_ = new internal::signal_impl;
335
- impl_->reference (); // start with a reference count of 1
311
+ impl_ = std::make_shared<internal::signal_impl>();
336
312
}
337
313
return impl_;
338
314
}
0 commit comments