Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1867944
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:37:29+00:00 2026-05-17T06:37:29+00:00

I wrote an application using wxWidgets that uses wxList . I am having some

  • 0

I wrote an application using wxWidgets that uses wxList. 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()).

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.S: in the application, the list contains about 15 thousand items, as opposed to only 9 in the test.

#include <wx/list.h>
#include <wx/log.h>

class TestItem
{
public:
    TestItem(int _x, int _y) { x = _x; y = _y; }
    int x;
    int y;
};

WX_DECLARE_LIST(TestItem, TestList);

#include <wx/listimpl.cpp>
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->Append(item);
    }

    void PrintAll() {
        wxLogMessage(wxT("List size: %d"), list->GetCount());
        wxTestListNode *node = list->GetFirst();
        while (node) {
            TestItem *item = node->GetData();
            wxLogMessage(wxT("Item: %d, %d"), item->x, item->y);
            node = node->GetNext();
        }
    }

    void DeleteAllX(int x) {
        wxTestListNode *node = list->GetFirst();
        while (node) {
            TestItem *item = node->GetData();
            if (item->x != x) {
                node = node->GetNext();
                continue;
            }
            wxTestListNode *toDelete = node;
            node = node->GetNext();
            wxLogMessage(wxT("Deleting item: %d, %d"), item->x, item->y);
            list->Erase(toDelete);
            delete item;
        }
    }

    void Clean() {
        list->DeleteContents(true);
        list->Clear();
    }

    static void DoAllTests() {
        Test *t = new Test;
        t->CreateAndAddToList(1, 1);
        t->CreateAndAddToList(1, 2);
        t->CreateAndAddToList(1, 3);
        t->CreateAndAddToList(2, 1);
        t->CreateAndAddToList(2, 2);
        t->CreateAndAddToList(2, 3);
        t->CreateAndAddToList(3, 1);
        t->CreateAndAddToList(3, 2);
        t->CreateAndAddToList(3, 3);
        t->PrintAll();
        t->DeleteAllX(2);
        t->PrintAll();
        t->Clean();
        t->PrintAll();
        delete t;
    }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:37:30+00:00Added an answer on May 17, 2026 at 6:37 am

    On the difference between list->GetFirst() and list->begin() in wxList API, it seems to be that list->GetFirst() returns NULL if list is empty and list->begin() returns the value of iterator to end list->end() as usual for other iterators. list->GetFirst() is the old API, list->begin() is the new one. The main benefit is that is allow you to use templates expecting an iterator with a wxList.

    wxList is considered as deprecated and to be replaced by std::list, but that should not worry you too much as it is done internally with new versions of wx (wxList just become a thin wrapper over wxList).

    The way you use it seems fine anyway and I see no obvious error in DeleteAllX(), even if it can be slightly simplified.

    What I would suspect is that some previous memory allocation failed (can be very silent if it was done through malloc) and cause havoc in the list at a later time when you delete, or that the segfault occurs inside the destructor of your own objects when you call delete. As many programming errors can lead to that situation it looks more likely to me than some problem from wxList, including allocation problems. However that is easy enough to check, just track calls to your destructors and you knwo soon enough if segfaults came from there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.