Passing a lambda function to a base class in the constructor causes NullReferenceException when the function is called - by jdh28

Status : 

  Won't Fix<br /><br />
		Due to several factors the product team decided to focus its efforts on other items.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


0
0
Sign in
to vote
ID 471135 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 6/29/2009 7:31:08 AM
Access Restriction Public

Description

I have a base class that takes some delegate in its constructor. If I pass in a lambda function in the constructor of some derived class which references a method of the class, when the delegate is called later, a NullReferenceException is thrown.

I suspect that the implicit 'this' field is not being captured correctly in the closure.
Sign in to post a comment.
Posted by Thomas Levesque on 3/15/2010 at 6:05 PM
Although this bug was closed as "won't fix", it seems to be fixed eventually : the C# 4 compiler gives an error when you try to do that. So you could change the status of this bug to "fixed" ;)
Posted by Microsoft on 7/8/2009 at 12:13 PM
Thanks for the bug report for Visual Studio 2008!

Using this mechanism, you can indeed sneak the this pointer into a base call, which is something we otherwise try pretty hard to prevent! This is definitely code that should be a compiler error, but unfortunately, we're starting to lock down on what we can fix for the next release and improving the experience for error situations falls below the bar right now.

We'll definitely keep it in mind to clean up base calls in lambdas in our next release!

Alex Turner
Program Manager
Visual C# Compiler
Posted by Microsoft on 6/29/2009 at 9:41 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.

Thank you