title: VC2013: my app crashes trying to lock a mutex
body: <p>I receive crash dumps. Some of them has the following stack trace:</p>
  
  <pre><code>msvcr120.dll!Concurrency::details::_RegisterConcRTEventTracing() Line 240   C++
  msvcr120.dll!Concurrency::details::SchedulerBase::CreateWithoutInitializing(const Concurrency::SchedulerPolicy &amp; policy) Line 283   C++
  msvcr120.dll!Concurrency::details::SchedulerBase::CreateContextFromDefaultScheduler() Line 571  C++
  msvcr120.dll!Concurrency::details::SchedulerBase::CurrentContext() Line 404 C++
  msvcr120.dll!Concurrency::critical_section::lock() Line 1031    C++
  msvcp120.dll!mtx_do_lock(_Mtx_internal_imp_t * * mtx, const xtime * target) Line 67 C++
  app.exe!vmsRaisesVaEvents::SubscribeToVaEvents(vmsConsumesVaEvents * pSubscriber) Line 14   C++
  app.exe!vmsSiHtml5UiApp::MakeSureSplashCreated(CSiHtml5AppStartingSplash::SplashScreenMode ssmRequired) Line 458    C++
  app.exe!vmsSiHtml5UiApp::PerformWbaPreInitializationIfRequired() Line 63    C++
  app.exe!vmsSiHtml5UiApp::RunAsNonUiAuxiliaryProcess() Line 140  C++
  app.exe!vmsSiApp::Run() Line 106    C++
  app.exe!wWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, wchar_t * lpCmdLine, int nCmdShow) Line 23   C++
  [External Code] 
  [Frames may be missing, no binary loaded for ntdll.dll] 
  ntdll.dll!0000000076ed3021()    Unknown
  </code></pre>
  
  <p>My app is just starting and initializing. It has one thread only (main thread) of my app and 5 other threads which were created by something I don't know. But they does not belong to my code.</p>
  
  <p>In MakeSureSplashCreated function I create object. </p>
  
  <pre><code>m_spSplashMgr = std::make_shared &lt;vmsSiHtml5AppStartingSplashMgr&gt;
  </code></pre>
  
  <p>And subscribe to its events:</p>
  
  <pre><code>m_spSplashMgr-&gt;SubscribeToVaEvents (this);
  </code></pre>
  
  <p>Object is derived from vmsRaisesVaEvents class to provide this functionality. Its code:</p>
  
  <pre><code>class vmsRaisesVaEvents
  {
  public:
      vmsRaisesVaEvents () :
          m_rasingEvents (0) {}
  
      virtual ~vmsRaisesVaEvents () {}
  
      void SubscribeToVaEvents (vmsConsumesVaEvents *pSubscriber)
      {
          std::unique_lock &lt;std::mutex&gt; lock (ms_events);
          wait_for_events_raising_complete (lock);
          assert (std::find (m_vSubscribers.begin (), m_vSubscribers.end (), pSubscriber) == m_vSubscribers.end ());
          m_vSubscribers.push_back (pSubscriber);
      }
  
      void UnsubscribeFromVaEvents (vmsConsumesVaEvents *pSubscriber)
      {
          std::unique_lock &lt;std::mutex&gt; lock (ms_events);
          wait_for_events_raising_complete (lock);
          auto it = std::find (m_vSubscribers.begin (), m_vSubscribers.end (), pSubscriber);
          assert (it != m_vSubscribers.end ());
          if (it != m_vSubscribers.end ())
              m_vSubscribers.erase (it);
      }
  
  private:
      static std::mutex ms_events;
      unsigned m_rasingEvents;
  
  protected:
      std::vector &lt;vmsConsumesVaEvents*&gt; m_vSubscribers;
  
  protected:
      void wait_for_events_raising_complete (std::unique_lock &lt;std::mutex&gt; &amp;lock)
      {
          for (;;)
          {
              if (!m_rasingEvents)
                  return;
              lock.unlock ();
              Sleep (0);
              lock.lock ();
          }
      }
  
      void begin_raise_events ()
      {
          std::unique_lock &lt;std::mutex&gt; lock (ms_events);
          m_rasingEvents++;
      }
  
      void end_raise_events ()
      {
          std::unique_lock &lt;std::mutex&gt; lock (ms_events);
          assert (m_rasingEvents);
          m_rasingEvents--;
      }
  
  protected:
      void RaiseEvent (unsigned nEvent, ...)
      {
          va_list args;
          va_start (args, nEvent);
  
          begin_raise_events ();
  
          for (auto it = m_vSubscribers.begin (); it != m_vSubscribers.end (); ++it)
              (*it)-&gt;onVaEvent (this, nEvent, args);
  
          end_raise_events ();
  
          va_end (args);
      }
  };
  
  __declspec(selectany) std::mutex vmsRaisesVaEvents::ms_events;
  </code></pre>
  
  <p>I get crash in vmsRaisesVaEvents::SubscribeToVaEvents on this line:</p>
  
  <pre><code>std::unique_lock &lt;std::mutex&gt; lock (ms_events);
  </code></pre>
  
  <p>Why so? The only suspecting thing is:</p>
  
  <pre><code>__declspec(selectany) std::mutex vmsRaisesVaEvents::ms_events;
  </code></pre>
  
  <p>Other than this, all objects must be in fine state (exists, not damaged, etc.).</p>