Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

Why C# compiler generates single class to capture variables of several lambdas?

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?

P.S. Somewhat more detailed, in my blog: here and here.

question from:https://stackoverflow.com/questions/12009309/why-c-sharp-compiler-generates-single-class-to-capture-variables-of-several-lamb

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have rediscovered a known shortcoming in the implementation of anonymous functions in C#. I described the problem in my blog in 2007.

Is there a way to convince compiler to produce two different capture classes?

No.

Or any reason why it cannot be done?

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...