Global dynamic FIleLogger initialization throws exception at CriticalSection::enter()

Recently I’ve added a FileLogger to my JUCE-based Application. The logger object is created dynamically, stored in a std::unique_ptr with a custom deleter. The code is as below:

.GLogger.cpp

#include "GLogger.h"

juce::FileLogger* initLogger()
{
    FileLogger *logger = FileLogger::createDateStampedLogger("logs", "App-", ".log", "App log file.");
    Logger::setCurrentLogger(logger);

    DBG("Log file path: " + logger->getLogFile().getFullPathName());

    return logger;
}

/**
 * Custom deleter detaching current logger before deletion
 *
 * Needs to be employed so that JUCE does not throw assertion errors.
 */
void detachAndDestroyLogger(juce::FileLogger *logger)
{
    juce::Logger *currentLogger = juce::Logger::getCurrentLogger();

    if (currentLogger == logger)
        juce::Logger::setCurrentLogger(nullptr);

    delete logger;
}

/* custom unique_ptr to enable the custom deleter */
using uniqueFileLogger = std::unique_ptr<juce::FileLogger, decltype(&detachAndDestroyLogger)>;
uniqueFileLogger logger(initLogger(), detachAndDestroyLogger);

I’ve also changed the DBG macro to call Logger::writeToLog instead of Logger::outputDebugString. This is all fine on Linux, the DBG macro writes to a file, however on windows a series of errors occur and I’m struggling to identify the cause.

The Call Stack is as below:

 	ntdll.dll!00007ff9746b49e6()	Unknown
 	ntdll.dll!00007ff97467fcb4()	Unknown
 	ntdll.dll!00007ff97467fae2()	Unknown
>	App.exe!juce::CriticalSection::enter() Line 50	C++
 	App.exe!juce::GenericScopedLock<juce::CriticalSection>::GenericScopedLock<juce::CriticalSection>(const juce::CriticalSection & lock) Line 67	C++
 	App.exe!juce::Array<void *,juce::CriticalSection,0>::add(void * && newElement) Line 430	C++
 	App.exe!juce::OutputStream::OutputStream() Line 64	C++
 	App.exe!juce::FileOutputStream::FileOutputStream(const juce::File & f, unsigned __int64 bufferSizeToUse) Line 31	C++
 	App.exe!juce::File::create() Line 504	C++
 	App.exe!juce::FileLogger::FileLogger(const juce::File & file, const juce::String & welcomeMessage, const __int64 maxInitialFileSizeBytes) Line 35	C++
 	App.exe!juce::FileLogger::createDateStampedLogger(const juce::String & logFileSubDirectoryName, const juce::String & logFileNameRoot, const juce::String & logFileNameSuffix, const juce::String & welcomeMessage) Line 126	C++
 	App.exe!initLogger() Line 15	C++
 	App.exe!`dynamic initializer for 'logger''() Line 39	C++
 	[External Code]	

The app halts with an unhandled exception:

Unhandled exception at 0x00007FF9746B49E6 (ntdll.dll) in App.exe: 0xC0000005: Access violation writing location 0x0000000000000024.

Any obvious reasons why that could be happening?

This is almost certainly due to undefined-order-of-initialisation of namespace-scope objects in different translation units. Creating objects with cross-translation-unit dependencies at namespace scope is normally a bad idea (more info here). Maybe you could try using a SharedResourcePointer to store your custom logger, then creating it after the rest of your app has initialised.