title: What is the proper way of handling linked lists in wxWidgets?
body: <p>I wrote an application using wxWidgets that uses <a href="http://docs.wxwidgets.org/stable/wx_wxlist.html#wxlist" rel="nofollow">wxList</a>. I am having some random crahses (segfault) in the destructors that collect the list data. I could not find a definite way of removing items from the list (Erase() VS DeleteNode()). Even iterating over the items has two flavours (list->GetFirst() VS list->begin()).</p>
  
  <p>Below is a test class showing the approach I am using in my application. The test runs perfectly, no crash. It seems like some pointers are being used after they are freed, but I can not tell that by looking at the code. I suppose am doing something wrong around the Erase() and DeleteContents() calls.</p>
  
  <p>P.S: in the application, the list contains about 15 thousand items, as opposed to only 9 in the test.</p>
  
  <pre><code>#include &lt;wx/list.h&gt;
  #include &lt;wx/log.h&gt;
  
  class TestItem
  {
  public:
      TestItem(int _x, int _y) { x = _x; y = _y; }
      int x;
      int y;
  };
  
  WX_DECLARE_LIST(TestItem, TestList);
  
  #include &lt;wx/listimpl.cpp&gt;
  WX_DEFINE_LIST(TestList);
  
  class Test {
  
  public:
      TestList *list;
      Test() {
          list = new TestList;
      }
  
      ~Test() {
          Clean();
          delete list;
      }
  
  
      void CreateAndAddToList(int x, int y) {
          TestItem *item = new TestItem(x, y);
          list-&gt;Append(item);
      }
  
      void PrintAll() {
          wxLogMessage(wxT("List size: %d"), list-&gt;GetCount());
          wxTestListNode *node = list-&gt;GetFirst();
          while (node) {
              TestItem *item = node-&gt;GetData();
              wxLogMessage(wxT("Item: %d, %d"), item-&gt;x, item-&gt;y);
              node = node-&gt;GetNext();
          }
      }
  
      void DeleteAllX(int x) {
          wxTestListNode *node = list-&gt;GetFirst();
          while (node) {
              TestItem *item = node-&gt;GetData();
              if (item-&gt;x != x) {
                  node = node-&gt;GetNext();
                  continue;
              }
              wxTestListNode *toDelete = node;
              node = node-&gt;GetNext();
              wxLogMessage(wxT("Deleting item: %d, %d"), item-&gt;x, item-&gt;y);
              list-&gt;Erase(toDelete);
              delete item;
          }
      }
  
      void Clean() {
          list-&gt;DeleteContents(true);
          list-&gt;Clear();
      }
  
      static void DoAllTests() {
          Test *t = new Test;
          t-&gt;CreateAndAddToList(1, 1);
          t-&gt;CreateAndAddToList(1, 2);
          t-&gt;CreateAndAddToList(1, 3);
          t-&gt;CreateAndAddToList(2, 1);
          t-&gt;CreateAndAddToList(2, 2);
          t-&gt;CreateAndAddToList(2, 3);
          t-&gt;CreateAndAddToList(3, 1);
          t-&gt;CreateAndAddToList(3, 2);
          t-&gt;CreateAndAddToList(3, 3);
          t-&gt;PrintAll();
          t-&gt;DeleteAllX(2);
          t-&gt;PrintAll();
          t-&gt;Clean();
          t-&gt;PrintAll();
          delete t;
      }
  };
  </code></pre>