-
-
Notifications
You must be signed in to change notification settings - Fork 746
refactor: avoid capturing this
in delegate using local self pointer
#10821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Thanks for your pull request and interest in making D better, @gorsing! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + phobos#10821" |
Thanks for your contribution, @gorsing :) Would you mind adding a unittest that showcases the advantage of this change? (Assuming that this is possible. If not, don’t bother.) Doing so could also potentially prevent accidental breakage caused by future changes. |
@0xEAB Changes aimed at eliminating leaks in closures |
Hi @CyberShadow , how long merge will be review? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This introduces more complexity in an already fairly-complex bit of code. I can't approve this in good faith.
The cast to void*
and back is especially scary. I wouldn't envy anyone who would need to look at this code in the future, after this patch.
Could we do something else here? For example:
static struct HeaderReceiver
{
// add any necessary state here...
// ...
void receiveHeader(in char[] header)
{
// ... put logic here ...
}
}
HeaderReceiver headerReceiver;
headerReceiver.stateVar1 = ...; // maybe even put &this here if really needed
curl.onReceiveHeader = &headerReceiver.receiveHeader;
// ...
To avoid implicit capturing of
this
in a delegate,replaced direct field access with access via a local pointer
self
cast toImpl*
.Old similar and alternative #10816
This is a preventive fix for a potential Phobos error related to delegate lifetime,
as seen in: dlang/dmd#21514
No functional behavior change is intended.