title: C++ std::mutex lock() access violation in Visual Studio 2017
body: <p>When I'm trying to run executable compiled using VS2017 I catch
  <code>Exception thrown at 0x00007FFF05BC1063 (msvcp140d.dll) in a.exe: 0xC0000005: Access violation reading location 0x0000000000000000.</code> immediately after launching.</p>
  
  <p>After debugging i figured out that it happens when I'm trying to lock static mutex _coutMutex. How can I fix it because when I have compiled using mingw it worked fine. Here is part of my code:</p>
  
  <p>Game.hpp:
  </p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include "Logger.hpp"
  
  class Game
  {
  public:
      static Logger logger;
  };
  </code></pre>
  
  <p>Game.cpp:
  </p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include "Game.hpp"
  
  Logger Game::logger{ "logs/client", Logger::LoggingLevels::Info, 
    Logger::LoggingLevels::Trace, 2, 100 };
  </code></pre>
  
  <p>Logger.hpp:
  </p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;mutex&gt;
  
  class Logger
  {
  public:
      Logger(std::string path, short consoleLoggingLevel, short 
        fileLoggingLevel, uint32_t count, size_t maxSize);
  
      enum LoggingLevels : short
      {
          Off = 0,
          Fatal = 1,
          Error = 2,
          Warn = 3,
          Info = 4,
          Debug = 5,
          Trace = 6
      };
  
  void _addToQueue(std::string data);
  private:
      static std::mutex _coutMutex;  
  };
  </code></pre>
  
  <p>Logger.cpp:
  </p>
  
  <pre class="lang-cpp prettyprint-override"><code>std::mutex Logger::_coutMutex;
  
  Logger::Logger(std::string path, short consoleLoggingLevel,
     short fileLoggingLevel, uint32_t count, size_t maxSize)
  {
      _addToQueue("dd/mm/yyyy hh:mm:ss.sss\n");
  }
  
  void Logger::_addToQueue(std::string data)
  {
      _coutMutex.lock();
      std::cout &lt;&lt; data;
      _coutMutex.unlock();
  }
  </code></pre>
  
  <p>main.cpp:
  </p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include "Logger.hpp"
  #include "Game.hpp"
  
  int main()
  {
      Game game;
  }
  </code></pre>