title: Why is this shared_ptr throwing an assert when it goes out of scope?
body: <p>Why does the following code trigger an assert? This code initially worked and at some point started triggering an assert as the shared_ptr fell out of scope.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;memory&gt;
  #include "GLFW/glfw3.h"
  int main()
  {
      if (!glfwInit()){
          std::cout &lt;&lt; "Failed to initialize GLFW." &lt;&lt; std::endl;
          return -1;
      }
      auto window = std::shared_ptr&lt;GLFWwindow&gt;(glfwCreateWindow(1024, 768, "Test", NULL, NULL));
      return 0;
  }
  </code></pre>
  
  <p>I've taken just the minimum amount of code that I can to reproduce this, and maybe I misunderstand the use of shared_ptr. I've also tried it with the syntax of: </p>
  
  <pre><code>std::shared_ptr&lt;GLFWwindow&gt; window(glfwCreateWindow(1024, 768, "Test", NULL, NULL));
  </code></pre>
  
  <p>The exact error message I am getting in the output window of the debugger (VS2013) is as follows:</p>
  
  <pre><code>Debug Assertion Failed!
  
  Program: C:\Users\...\xxxx.exe
  File: f:\dd\vctools\crt\crtw32\misc\dbgdel.cpp
  Line: 52
  
  Expression: _BLOCK_TYPE_IS_VALID(pHead-&gt;nBlockUse)
  </code></pre>
  
  <p>From what I've researched it seems that it is trying to free the shared_ptr twice - is this the case and how can I prevent that? It may be worth mentioning that swapping the type from <code>GLFWwindow</code> to <code>struct test { int i; };</code> no longer triggers the assert. Does this mean that GLFWwindow is deleting the pointer internally? If so why did the code work at one point but not now?</p>