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;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…