Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper: Implicitly captured closure: this

Tags:

c#

.net

resharper

I am getting this warning ("Implicity captured closure: this") from Resharper: does this mean that somehow this code is capturing the entire enclosing object?

    internal Timer Timeout = new Timer
                            {
                                Enabled = false,
                                AutoReset = false
                            };
    public Task<Response> ResponseTask
    {
        get
        {
            var tcs = new TaskCompletionSource<Response>();

            Timeout.Elapsed += (e, a) => tcs.SetException(new TimeoutException("Timeout at " + a.SignalTime));

            if (_response != null) tcs.SetResult(_response);
            else ResponseHandler += r => tcs.SetResult(_response);
            return tcs.Task;
        }
    }

I'm not sure how or why it's doing so - the only variable it should be capturing is the TaskCompletionSource, which is intentional. Is this actually a problem and how would I go about solving it if it is?

EDIT: The warning is on the first lambda (the Timeout event).

like image 686
Aaron Maslen Avatar asked Oct 18 '12 11:10

Aaron Maslen


2 Answers

It seems that the problem isn't the line I think it is.

The problem is that I have two lambdas referencing fields in the parent object: The compiler generates a class with two methods and a reference to the parent class (this).

I think this would be a problem because the reference to this could potentially stay around in the TaskCompletionSource object, preventing it from being GCed. At least that's what I've found on this issue suggests.

The generated class would look something like this (obviously names will be different and unpronounceable):

class GeneratedClass {
    Request _this;
    TaskCompletionSource tcs;

    public lambda1 (Object e, ElapsedEventArgs a) {
        tcs.SetException(new TimeoutException("Timeout at " + a.SignalTime));
    }

    public lambda2 () {
        tcs.SetResult(_this._response);
    }
}

The reason the compiler does this is probably efficiency, I suppose, as the TaskCompletionSource is used by both lambdas; but now as long as a reference to one of those lambdas is still referenced the reference to Request object is also maintained.

I'm still no closer to figuring out how to avoid this issue, though.

EDIT: I obviously didn't think through this when I was writing it. I solved the problem by changing the method like this:

    public Task<Response> TaskResponse
    {
        get
        {
            var tcs = new TaskCompletionSource<Response>();

            Timeout.Elapsed += (e, a) => tcs.SetException(new TimeoutException("Timeout at " + a.SignalTime));

            if (_response != null) tcs.SetResult(_response);
            else ResponseHandler += tcs.SetResult; //The event passes an object of type Response (derp) which is then assigned to the _response field.
            return tcs.Task;
        }
    }
like image 58
Aaron Maslen Avatar answered Nov 15 '22 23:11

Aaron Maslen


It looks like _response is a field in your class.

Referencing _response from the lambda will capture this in the closure, and will read this._response when the lambda executes.

To prevent this, you can copy _response to a local variable and use that instead. Note that that will cause it to use the current value of _response rather than its eventual value.

like image 31
SLaks Avatar answered Nov 15 '22 22:11

SLaks