Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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.

like image 627
Ivan Danilov Avatar asked Aug 17 '12 16:08

Ivan Danilov


People also ask

Why is C used?

C is a general-purpose programming language and can efficiently work on enterprise applications, games, graphics, and applications requiring calculations, etc. C language has a rich library which provides a number of built-in functions. It also offers dynamic memory allocation.

Why should you learn C?

C is very fast in terms of execution time. Programs written and compiled in C execute much faster than compared to any other programming language. C programming language is very fast in terms of execution as it does not have any additional processing overheads such as garbage collection or preventing memory leaks etc.

Why C language is named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C". C is about the tone C.


2 Answers

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.

like image 186
Eric Lippert Avatar answered Oct 06 '22 01:10

Eric Lippert


I'm fairly sure you are seeing practical limitations in the compiler's code rewriting logic, it is not simple to do. The workaround is easy enough, create the lambda in a separate method so you get two separate instances of the hidden class:

public static void DoSomething(object a, object b, Observer observer, Receiver r) {     var rCopy = r;     observer.X += register(r, a);     observer.X += register(rCopy, b); } private static EventHandler register(Receiver r, object obj) {     return new EventHandler((s, e) => r.Method(obj)); } 
like image 25
Hans Passant Avatar answered Oct 05 '22 23:10

Hans Passant