Ticket S136710
Visible to All Users

Code Issues - Add warning about accessing modified variable in closure

created 14 years ago

Hi,
There is a common code trap, a dev should be aware of:
One need to declare local temp variable to prevent closure from accessing mutated variable when declaring closure within loops.
CodeRush could give a hand - display a code issue (probably warning) and suggest "Introduce Local" as a fix.
See below links for more details on this problem:
http://msmvps.com/blogs/peterritchie/archive/2008/10/27/closure-tip.aspx
http://weblogs.asp.net/fbouma/archive/2009/06/25/linq-beware-of-the-access-to-modified-closure-demon.aspx
http://jarloo.com/news/bugs/access-to-modified-closure/
http://stackoverflow.com/questions/304258/access-to-modified-closure-2
Please note, that "Introduce Local" is NOT available today when one is aware of problem and wants to fix it by hand using CR.
Regards, Przemyslaw
Proposed Solution:
Display warning when closure is accessing modified variable.
Suggest "Introduce Local" as a fix.
"Introduce Local" should be available for variable that is modified within loop.

Show previous comments (2)
Alex Skorkin (DevExpress) 12 years ago

    Hi Rennie,
    This suggestion is still in our TODO list for future product updates. Currently, I am unable to provide you with an estimated time frame.

      Thanks for your reply. I guess it's become a bit less of a problem due to C# 5.0, which fixes the problem for "foreach", but the info I've seen says that the problem still exists for "for".
      http://stackoverflow.com/questions/304258/access-to-modified-closure-2

      Alex Skorkin (DevExpress) 12 years ago

        Thank you for the update, Rennie. We will take into account your notes when implementing this feature.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.