x64 build static variable doesn't initialize while debugged in VS

Article ID: 2799612 - View products that this article applies to.
Expand all | Collapse all

Summary

In Visual Studio 2010 SP1, if we build the application in the x64/Any CPU mode, we see that the static variable initialization does not happen under the debug mode.

If we create a console application using the following code snippet and putting the break point on the "private static bool publishEvent = true", we see the value of the static variable does not change, while stepping into the code. The value of the static variable remains uninitialized, in this case, false.

The expected behavior is, the value of publishEvent becomes true.


class Test
    {
        private static bool publishEvent = true;
       
        public Test()
        {
            Console.WriteLine("{0}", publishEvent);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
           Test varT = new Test();
           Console.ReadLine();
        }
    }

More information

This behavior of Visual Studio 2010 SP1 is a known issue. This issue is fixed in the next release of Visual Studio versions. However there are some easy workarunds available as mentioned below:
  • Put a break point on some other location, rather than the static variable declaration itself.
  • Put a [ThreadStatic] attribute before declaring the static variable.
  • Install .NET 4.5 Framework. You do not have to target 4.5 framework but just install it.

Note This is a "FAST PUBLISH" article created directly from within the Microsoft support organization. The information contained herein is provided as-is in response to emerging issues. As a result of the speed in making it available, the materials may include typographical errors and may be revised at any time without notice. See Terms of Use for other considerations.

Properties

Article ID: 2799612 - Last Review: January 16, 2013 - Revision: 1.0
Applies to
  • Microsoft Visual Studio 2010 Service Pack 1
Keywords: 
KB2799612

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com