Uh oh!
There was an error while loading. Please reload this page.
Fix accessing destroyed objects in the callback of async_wait - #362
Merged
BewareMyPower merged 3 commits intoDec 6, 2023
Conversation
Fixesapache#358Fixesapache#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(); } }); ```
BewareMyPowerforce-pushed
the
bewaremypower/safe-timer
branch
from
December 4, 2023 13:11
c093610 to
0729a9eComparemerlimat
approved these changes
Dec 4, 2023
BewareMyPower
marked this pull request as draft
December 5, 2023 02:06
BewareMyPower
commented
Dec 5, 2023
ContributorAuthor
It seems some tests failed. I will fix them ASAP. |
BewareMyPower
marked this pull request as ready for review
December 5, 2023 03:44
BewareMyPower
marked this pull request as draft
December 5, 2023 05:43
BewareMyPower
marked this pull request as ready for review
December 5, 2023 05:58
BewareMyPower added a commit
that referenced
this pull request
Dec 6, 2023
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(); } }); ``` (cherry picked from commit 24ab12c)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes#358Fixes#359
Motivation
async_waitis not used correctly in some places. A callback that captures thethispointer or reference tothisis passed toasync_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_waitcalls.std::weak_ptr<T> weakSelf{shared_from_this()}; timer_->async_wait([weakSelf](/* ... */) { if (auto self = weakSelf.lock()) { self->foo(); } });