Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7866869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:32:13+00:00 2026-06-03T00:32:13+00:00

My App was running fine when I started it from inside Visual Studio with

  • 0

My App was running fine when I started it from inside Visual Studio with the debugger attached (F5). But when I started the App without attaching the debugger (Ctrl-F5 or starting the .exe file) i did always get a StackOverflowException which was luckily logged in the Windows event logs.

The problematic code was the following:

namespace Caliburn.Micro.Contrib
{
    public static class FrameworkExtensions
    {
        public static class ViewLocator
        {
            static readonly Func<string,object, IEnumerable<string>> _baseTransformName = Micro.ViewLocator.TransformName;

            public static void EnableContextFallback()
            {
                Caliburn.Micro.ViewLocator.TransformName = FallbackNameTransform;
            }    

            static IEnumerable<string> FallbackNameTransform(string typeName, object context)
            {
                var names = _baseTransformName(typeName, context);
                if (context != null)
                {
                    names = names.Union(_baseTransformName(typeName, null));
                }

                return names;
            }
        }
    }
}

I called the FrameworkExtensions.EnableContextFallack() method during the App startup and the StackOverflowException occured during the first invocation of Caliburn.Micro.ViewLocator.TransformName. This means that the _baseTransformName variable is initialized after EnableContextFallback() was called when no debugger is attached and before EnableContextFallback() is called when a debugger is attached.

Is was able to fix the bug by adding a static constructor and assign the variable in the constructor

namespace Caliburn.Micro.Contrib
{
    public static class FrameworkExtensions
    {
        public static class ViewLocator
        {
            static readonly Func<string, object, IEnumerable<string>> _baseTransformName;

            static ViewLocator()
            {
                 _baseTransformName = Micro.ViewLocator.TransformName;
            }

            public static void EnableContextFallback()
            {
                Caliburn.Micro.ViewLocator.TransformName = FallbackNameTransform;
            }    

            static IEnumerable<string> FallbackNameTransform(string typeName, object context)
            {
                var names = _baseTransformName(typeName, context);
                if (context != null)
                {
                    names = names.Union(_baseTransformName(typeName, null));
                }

                return names;
            }
        }
    }
}

This ensures that the variable _baseTransformName is always set before the first invocation of EnableContextFallback().

So the question is: Why is there a different initialization behavior for static variables when a debugger is attached and is there a way to “disable” the different behavior?

cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:32:17+00:00Added an answer on June 3, 2026 at 12:32 am

    So the question is: Why is there a different initialization behavior for static variables when a debugger is attached and is there a way to “disable” the different behavior?

    Very little is guaranteed about the behaviour of static variable initializers when there’s no static constructor. Indeed, you can even create instances of classes without the static variable initializers being invoked! When you’re using a debugger, the CLR does all kinds of things differently (particularly around JITting).

    Using a static constructor is probably the best way of giving you more predictable initialization behaviour.

    See my blog post about type initialization changes in .NET 4 for more information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Spotify iPhone app I was working on, was running fine, but now I
I am installing inApp purchase in My app.Yesterday code was running fine and I
I got an app running on my SQL Server that is starting to slow
So... this is beyond strange. I had an app that was running perfectly fine
I have a web service that's been running fine without modification for a couple
I am developing a nice application that seems to be running fine, I've started
I decided to test my app in the production environment today. It's running fine
I'm running a simple express app, it is pulling static files from S3, and
I have an iPhone app running in the simulator that won't quit. I also
I've got an app running maximized in a borderless window and need access to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.