title: C++ Vectors and Memory Leaks
body: <p>I've read somewhere sometime ago that <strong>vectors</strong> cause memory leaks depending on how they're used, but I'd like to ask just to be sure:</p>
  
  <pre><code>#define _CRTDBG_MAP_ALLOC
  #include &lt;stdlib.h&gt;
  #include &lt;crtdbg.h&gt;
  
  int main()
  {
      vector&lt;somePtr*&gt; listPtrs;
  
      return 0;
      //Detects memory leaks
  }
  </code></pre>
  
  <p>And this doesn't detect anything:</p>
  
  <pre><code>#define _CRTDBG_MAP_ALLOC
  #include &lt;stdlib.h&gt;
  #include &lt;crtdbg.h&gt;
  
  int main()
  {
      {
          vector&lt;somePtr*&gt; listPtrs;
      }
  
      return 0;
      //No memory leaks detected
  }
  </code></pre>
  
  <p>The objects are being deleted from the vectors before clearing the pointers.
  I think I remember reading that vectors and lists and other std containers are auto-deleted after the block they're in, so in the example 1 I get the memory leak because the memory leak function is called before the block ends, so the vector is still alive and causing it.</p>
  
  <p>I'm not sure about it though, it's been quite a while since I think I read this, and I could only find questions about objects not being deleted, just pointers being removed.</p>
  
  <p>Is this true? And will I get memory leaks if I use global classes that contains vectors and lists?</p>