# CriticalSection crashes with VS 2022

**URL:** <https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737>\
**Category:** Windows\
**Created:** [October 6, 2024, 1:16pm UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737 "2024-10-06T13:16:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ans](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ans/32/617_2.png) [@ans](https://forum.juce.com/u/ans)\
**Post date:** [October 6, 2024, 1:16pm UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/1 "2024-10-06T13:16:11Z")

</div>

After updating from VS 2019 to VS 2022 and exporting the project with Projucer, I get this crash after starting the app.

 ![Screenshot](https://us1.discourse-cdn.com/flex026/uploads/juce/original/3X/0/b/0b197feb2bfca2cfeb97b01f014b3f6803e490bf.png)

It happens when the first singleton is instantiated. It’s the singleton’s lock that causes the crash. Larger context is during dynamic initialization of global variables very early in the startup. The singleton class inherits from DeletedAtShutdown.

What’s happening here? I assume many others have successfully used VS 2022 already, so something weird must begoing on here. What can I look for?

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [October 6, 2024, 1:50pm UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/2 "2024-10-06T13:50:37Z")

</div>

This sounds a little bit similar to the issue described in the linked thread, although that seemed to affect std::mutex rather than the Win32 mutex:

> [@Windows crash in APVTS constructor](https://forum.juce.com/t/windows-crash-in-apvts-constructor/62039/13):
>
> @adamwilson looking at this a little more I found the following in the [Microsoft STL release notes for VS 2022 17.10](https://github.com/microsoft/STL/wiki/Changelog#vs-2022-1710) Fixed mutex’s constructor to be constexpr. [#3824](https://github.com/microsoft/STL/pull/3824) [#4000](https://github.com/microsoft/STL/pull/4000) [#4339](https://github.com/microsoft/STL/pull/4339) Note: Programs that aren’t following the documented [restrictions on binary compatibility](https://learn.microsoft.com/en-us/cpp/porting/binary-compat-2015-2017?view=msvc-170) may encounter null dereferences in mutex machinery. You must follow this rule: When you mix binaries built by different supported versions of the toolset, the Redistributable version must be at least as new as the latest tool…

Regardless, if you have any library dependencies, it might be worth checking that you’re statically linking the most recent C++ runtime, or have the most recent dynamic runtime installed and available.

There’s some useful information about toolset compatibility on [this page](https://learn.microsoft.com/en-us/cpp/porting/binary-compat-2015-2017?view=msvc-170).

---

<div class="post-metadata">

**Author:** ![ans](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ans/32/617_2.png) [@ans](https://forum.juce.com/u/ans)\
**Post date:** [October 6, 2024, 4:50pm UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/3 "2024-10-06T16:50:55Z")

</div>

Thanks. The proposed workaround and solution didn’t help though.

The issue is probably that CriticalSection is not yet ready for use when C++ is initializing global application variables like, for example of this kind:

```auto

static inline const MyType namedGlobal = MySingleton::getInstance()->createSomething();

```

A minimal standalone app project that does nothing else should prove that. I will check that.

---

<div class="post-metadata">

**Author:** ![ans](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ans/32/617_2.png) [@ans](https://forum.juce.com/u/ans)\
**Post date:** [October 6, 2024, 5:10pm UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/4 "2024-10-06T17:10:13Z")

</div>

Yep. That’s it. This example reproduces the crash.

```auto

#include "JuceHeader.h"

class MySingleton
{
public:
    JUCE_DECLARE_SINGLETON (MySingleton, false)

    juce::String makeString() { return "Gotcha"; }    
};

static inline const juce::String namedGlobal = MySingleton::getInstance()->makeString();

```

Too bad. This wasn’t an issue with VS 2019 as far as I remember. I’m using lots of these variables e.g. for icons and other resources, in order to make sure misspellings are caught at compile time. If you look them up in a hash map by name you never learn about typos.

Not sure how to work around this. Maybe singletons could operate lock-free during initialization of global variables?

---

<div class="post-metadata">

**Author:** ![ans](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ans/32/617_2.png) [@ans](https://forum.juce.com/u/ans)\
**Post date:** [October 7, 2024, 8:58am UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/5 "2024-10-07T08:58:08Z")

</div>

Using **JUCE\_DECLARE\_SINGLETON\_SINGLETHREADED\_MINIMAL** looks like a workaround. But not for fonts. The Juce TypefaceCache is a regular singleton that crashes during startup.

Mutexes are not necessary while an app is initializing variables on a single thread. Still, if classes are involved that happen to use one that should not crash.

Would it make sense to file a bug with Microsoft?

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [October 7, 2024, 9:43am UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/6 "2024-10-07T09:43:54Z")

</div>

> [@ans](#):
>
> Would it make sense to file a bug with Microsoft?

Perhaps, if you have a minimal example (i.e. win32 only, no JUCE) that shows `EnterCriticalSection` crashing during static initialisation.

I’d be interested to see the full stack trace at the point of the crash to rule out other possibilities.

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [October 7, 2024, 10:47am UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/7 "2024-10-07T10:47:59Z")

</div>

Looking at the example you posted, I think the problem is due to initialisation order.

- `JUCE_DECLARE_SINGLETON` expands out to (amongst other things) a static data member declaration like:

- `JUCE_IMPLEMENT_SINGLETON` just adds a definition for this static data member in a translation unit:

- `namedGlobal` is initialisated at static initialisation time, as is the `singletonHolder`. If these statics are in different translation units, then their initialisation order is unspecified, so `namedGlobal` may be initialised before the singleton holder for `MySingleton`, leading to a crash.

To mitigate, you could:

- init `namedGlobal` lazily:

- or, ensure the correct initialisation order by declaring dependent statics in the same translation unit:

---

<div class="post-metadata">

**Author:** ![ans](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ans/32/617_2.png) [@ans](https://forum.juce.com/u/ans)\
**Post date:** [October 7, 2024, 12:06pm UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/8 "2024-10-07T12:06:44Z")

</div>

Thanks for taking the time to look into this. Here’s the stack trace.

 ![Screenshot2](https://us1.discourse-cdn.com/flex026/uploads/juce/original/3X/9/2/920065b1a6f70c377ba99b9046539008e0e2b69a.png)

Your proposed solution works. Unfortunately, maintaining a long list with dozens of variables across two files (header and cpp) is something to avoid. I’d rather have the entire list in the header only.

Not sure how to accomplish that yet. Might have to run my own header-only singleton wrapper. At least the cause is clear now.

---

<div class="post-metadata">

**Author:** ![ans](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ans/32/617_2.png) [@ans](https://forum.juce.com/u/ans)\
**Post date:** [October 7, 2024, 1:02pm UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/9 "2024-10-07T13:02:25Z")

</div>

Here’s a header-only simple singleton solution that works so far. The boilerplate can be wrapped in a macro. It has the advantage of not being dependent on initialization order, i.e. genuine “lazy” evaluation.

Static variables with local scope are extremely handy as an alternative to global variables. They spare you the separate “implementation” part.

```auto

#include "JuceHeader.h"
using namespace juce;

class MySingleton : DeletedAtShutdown
{
public:                

    static MySingleton* getInstance()
    {
        const ScopedLock sl (getLock());

        if (singletonHolder == nullptr)
            singletonHolder.reset (new MySingleton);
        return singletonHolder.get();
    }

    static void clearSingletonInstance()
    {
        singletonHolder.release();
    }

    ~MySingleton()
    {
        clearSingletonInstance();
    }

    String makeString() { return "Gotcha"; }  

private:
    static inline std::unique_ptr<MySingleton> singletonHolder = {};

    static CriticalSection& getLock()
    {
        static CriticalSection lock;
        return lock;
    }
};

static inline const String namedGlobal = MySingleton::getInstance()->makeString();

```

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [October 7, 2024, 1:29pm UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/10 "2024-10-07T13:29:34Z")

</div>

Do you need the DeletedAtShutdown behaviour? If not, I wonder why you don’t just use the standard C++11 pattern:

```auto
class MySingleton
{
public:
    static MySingleton& getInstance()
    {
        static MySingleton result;
        return result;
    }

private:
    MySingleton() = default;
};

```

---

<div class="post-metadata">

**Author:** ![ans](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ans/32/617_2.png) [@ans](https://forum.juce.com/u/ans)\
**Post date:** [October 7, 2024, 1:51pm UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/11 "2024-10-07T13:51:07Z")

</div>

For the most part DeletedAtShutdown is needed to avoid a barrage of leaked object warnings that obfuscate/dilute other leak warnings.

---

<div class="post-metadata">

**Author:** ![ans](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ans/32/617_2.png) [@ans](https://forum.juce.com/u/ans)\
**Post date:** [October 7, 2024, 2:15pm UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/12 "2024-10-07T14:15:41Z")

</div>

The current JUCE implementation with the SingletonHolder as a global variable separate from the header causes some stress here. For example, it’s impossible to store an object in a header-defined global variable that makes use of a Font, directly or indirectly. TypefaceCache is just not ready at the time and crashes.

If SingletonHolder was a static inline variable in the header, it would be initialised early and reliably before any user code gets involved. As said, getIInstance() should be callable at any time.

I assume it’s for historical reasons when static inline wasn’t available pre C++17, correct? Any reason for not moving it to the header now that C++17 is minium requirement? It shouldn’t affect any code out there and make things much more robust during startup.

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [October 7, 2024, 2:33pm UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/13 "2024-10-07T14:33:10Z")

</div>

> [@ans](#):
>
> For the most part DeletedAtShutdown is needed to avoid a barrage of leaked object warnings that obfuscate/dilute other leak warnings.

That is true and actually one of the most painful things when dealing with static in JUCE (any struct that might contain, say, juce::String in it, must be contained in a JUCE singleton instead of a regular singleton.

> [@ans](#):
>
> If SingletonHolder was a static inline variable in the header, it would be initialised early and reliably before any user code gets involved. As said, getIInstance() should be callable at any time.

To my knowledge, if you’re creating a static function that fetches a singleton it would mean the singleton is not shared with other translation units and gets duplicated for each TU.

I think the best way is to use:

1. Header:

```auto
MyGlobalObject& getObject();

```

1. CPP file:

```auto
MyGlobalObject& getObject()
{
    return *MyGlobalObject::getInstance();
}

```

1. Avoid any use of non-singleton globals as the static initialization order in C++ is UB.

I would also suggest to use `SharedResourcePointer<MyGlobalObject>` whenever possible to further simplify the usage pattern.

---

<div class="post-metadata">

**Author:** ![ans](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ans/32/617_2.png) [@ans](https://forum.juce.com/u/ans)\
**Post date:** [October 7, 2024, 3:56pm UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/14 "2024-10-07T15:56:15Z")

</div>

> [@eyalamir](#):
>
> To my knowledge, if you’re creating a static function that fetches a singleton it would mean the singleton is not shared with other translation units and gets duplicated for each TU.

Not if you use a **static inline** _variable_ in class scope (as of C++17). These get linked as a single instance across all compilation units. I’m using that all the time.

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [October 7, 2024, 4:11pm UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/15 "2024-10-07T16:11:31Z")

</div>

> [@ans](#):
>
> Not if you use a **static inline** _variable_ in class scope (as of C++17). These get linked as a single instance across all compilation units. I’m using that all the time.

I think you’re correct about that.

However - you would probably get the exact same effect - just with the extra safety of lazy creation and if you replace your static members with members that use `SharedResourcePointer<T>`

---

<div class="post-metadata">

**Author:** ![kunz](https://avatars.discourse-cdn.com/v4/letter/k/f19dbf/32.png) [@kunz](https://forum.juce.com/u/kunz)\
**Post date:** [October 8, 2024, 8:56am UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/16 "2024-10-08T08:56:37Z")

</div>

I would avoid static variables and singletons whenever possible and keep the state inside classes. There is no need for them.

---

<div class="post-metadata">

**Author:** ![ans](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ans/32/617_2.png) [@ans](https://forum.juce.com/u/ans)\
**Post date:** [October 8, 2024, 9:23am UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/17 "2024-10-08T09:23:39Z")

</div>

The purpose of a singleton is exactly that. To keep global state encapsulated in the sole instance of a class.

---

<div class="post-metadata">

**Author:** ![kunz](https://avatars.discourse-cdn.com/v4/letter/k/f19dbf/32.png) [@kunz](https://forum.juce.com/u/kunz)\
**Post date:** [October 8, 2024, 9:40am UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/18 "2024-10-08T09:40:12Z")

</div>

You can also have one class that holds the state without the singleton pattern. Passing a reference to other classes.

Most use singletons because they can read/change the state from everywhere. This has several drawbacks as you already noticed when reporting this bug. It’s also hard to write tests for classes that are using singletons.

But this is just my opinion. In plugins, we shouldn’t use them anyway.

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [October 8, 2024, 1:44pm UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/19 "2024-10-08T13:44:52Z")

</div>

> [@kunz](#):
>
> But this is just my opinion. In plugins, we shouldn’t use them anyway.

Singletons are extremely valuable for plugins for anything that has to be shared across instances.  
Examples are all the graphic assets (Images, Fonts, etc), most of which JUCE is already providing singletons for (like ImageCache).

There are more extreme scenarios. For example we make sample-based plugins where the samples have to be held be a Singleton to not eat up the user’s memory.

That’s also true for performance related objects like thread pools.

---

<div class="post-metadata">

**Author:** ![kunz](https://avatars.discourse-cdn.com/v4/letter/k/f19dbf/32.png) [@kunz](https://forum.juce.com/u/kunz)\
**Post date:** [October 9, 2024, 7:16am UTC](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737/20 "2024-10-09T07:16:41Z")

</div>

> [@eyalamir](#):
>
> Examples are all the graphic assets (Images, Fonts, etc), most of which JUCE is already providing singletons for (like ImageCache).

Yes, this is true. Sharing resources inside a plug-in makes sense, but I don’t want to share these resources between instances. I would prefer a clean state for every new plugin instance and keep things simple.

We now have some popular hosts that start the plugin in dedicated processes. In this case, you may experience different behavior in different DAWs.

For me, sharing things between instances adds complexity I don’t want.

Edit: I have tons of issues because of the static default look and feel class.

[Next page](https://forum.juce.com/t/criticalsection-crashes-with-vs-2022/63737.md?page=2)
