Assume we have such code:
public class Observer
{
public event EventHandler X = delegate { };
}
public class Receiver
{
public void Method(object o) {}
}
public class Program
{
public static void DoSomething(object a, object b, Observer observer, Receiver r)
{
var rCopy = r;
EventHandler action1 = (s, e) => rCopy.Method(a);
EventHandler action2 = (s, e) => r.Method(b);
observer.X += action1;
observer.X += action2;
}
public static void Main(string[] args)
{
var observer = new Observer();
var receiver = new Receiver();
DoSomething(new object(), new object(), observer, receiver);
}
}
Here action1 and action2 have completely separated set of captured variables – rCopy was created especially for this. Still, compiler generates just one class to capture everything (checked generated IL). I suppose it is done for optimization reasons, but it allows very hard-to-spot memory leak bugs: if a and b captured in single class, GC is unable to collect both at least so long as any of lambdas are referenced.
Is there a way to convince compiler to produce two different capture classes? Or any reason why it cannot be done?
You have rediscovered a known shortcoming in the implementation of anonymous functions in C#. I described the problem in my blog in 2007.
No.
There is no theoretical reason why an improved algorithm for partitioning closed-over variables so that they are hoisted into different closure classes could not be devised. We have not done so for practical reasons: the algorithm is complicated, expensive to get right and expensive to test, and we have always had higher priorities. Hopefully that will change in Roslyn, but we are making no guarantees.