title: Deleting objects in an stl list using the erase function in c++
body: <p>So I'm trying to implement some memory management techniques here. In my program, I created an stl list so that it would contain objects of the class "Blast" with it's respective iterator:</p>
  
  <pre><code>list&lt;Blast&gt; blasts;
  list&lt;Blast&gt;::iterator it1;
  </code></pre>
  
  <p>I've looked around and found that if you use the erase function, it will call the destructor of the class, and then delete the iterator to which it points to the object and return some other pointer (iterator). However, if the class is a POD, then the call for the destructor won't do anything, hence all the information that was saved in that object will be kept in memory.</p>
  
  <p>To go around this, I tried to allocate the object dynamically so that I could use the delete function to wipe it out of memory as such:</p>
  
  <pre><code>Blast *blast;
  blast = new Blast;
  </code></pre>
  
  <p>However, here I encountered a problem. When i tried to push the object onto the list, it only lets me push the pointer:</p>
  
  <pre><code>blasts.push_back(*blast);
  </code></pre>
  
  <p>At first, I didn't think anything about it. So I continued:</p>
  
  <pre><code>if(it2-&gt;getX() &gt; 600) {
  
     delete &amp;it2;
     it2 = blasts.erase(it2);
  
     }
  </code></pre>
  
  <p>I realized then, that what this does is that it erases the pointer that was pointing to the object, and then the pointer that was pointing to the pointer pointing to the object.</p>
  
  <p>I'm not entirely sure if that is actually correct, as I have no way of knowing if there's a memory leak. So my question is this:</p>
  
  <p>What do I have to do to erase the object from memory BEFORE deleting whatever pointer is pointing to that object? Is my implementation correct? Is it's result the same as if I were to just simply push the object onto the list? </p>