Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>We'll need to see your code for insertion and deletion to be sure about it.</p> <p>What I'd see as a memleak-free insert / remove code would be: ( NOTE: I'm assuming you don't store the Cells that you allocate in the map )</p> <pre><code>// // insert // std::map&lt;int, Cell&gt; _map; Cell a; // no new here! Cell *iter = &amp;a; while( condition ) { Cell *b = new Cell(); iter-&gt;next = b; iter = b; } _map[id] = a; // will 'copy' a into the container slot of the map // // cleanup: // std::map&lt;int,Cell&gt;::iterator i = _map.begin(); while( i != _map.end() ) { Cell &amp;a = i-&gt;second; Cell *iter = a.next; // list of cells associated to 'a'. while( iter != NULL ) { Cell *to_delete = iter; iter = iter-&gt;next; delete to_delete; } _map.erase(i); // will remove the Cell from the map. No need to 'delete' i++; } </code></pre> <p><strong>Edit</strong>: there was a comment indicating that I might not have understood the problem completely. If you insert ALL the cells you allocate in the map, then the faulty thing is that your map contains <code>Cell</code>, not <code>Cell*</code>. </p> <p>If you define your map as: <code>std::map&lt;int, Cell *&gt;</code>, your problem would be solved at 2 conditions:</p> <ol> <li>you insert <strong>all</strong> the Cells that you allocate in the map</li> <li>the integer (the key) associated to each cell is <strong>unique</strong> (important!!) </li> </ol> <p>Now the deletion is simply a matter of:</p> <pre><code>std::map&lt;int, Cell*&gt;::iterator i = _map.begin(); while( i != _map.end() ) { Cell *c = i-&gt;second; if ( c != NULL ) delete c; } _map.clear(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload