You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix accessing destroyed objects in the callback of async_wait
Fixes#358Fixes#359
### Motivation
`async_wait` is not used correctly in some places. A callback that
captures the `this` pointer or reference to `this` is passed to
`async_wait`, if this object is destroyed when the callback is called,
an invalid memory access will happen.
### Modifications
Use the following pattern in all `async_wait` calls.
```c++
std::weak_ptr<T> weakSelf{shared_from_this()};
timer_->async_wait([weakSelf](/* ... */) {
if (auto self = weakSelf.lock()) {
self->foo();
}
});
```
0 commit comments