How to delete std::vector<wxString*> ? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
cecilio
I live to help wx-kind
I live to help wx-kind
Posts: 189
Joined: Fri Dec 03, 2004 8:44 am
Location: spain
Contact:

How to delete std::vector<wxString*> ?

Post by cecilio » Tue Apr 15, 2008 8:42 am

What is the correct method to empty a vector of wxString pointers?

I'm trying this:

Code: Select all

std::vector<wxString*>::iterator it;
for (it = m_Labels.begin(); it != m_Labels.end(); ++it)
    delete *it;
m_Labels.clear();
But I'm getting a run time error in the 'delete *it' instruction:

Debug Assert Fail. File dbgdel.cpp, Line 52.
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

The vector is valid, as well as the contained wxString pointers.

Thank you,

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Tue Apr 15, 2008 9:02 am

use the indexoperator delete vec;

Or take a look at the boost::pointercontainer library.

cecilio
I live to help wx-kind
I live to help wx-kind
Posts: 189
Joined: Fri Dec 03, 2004 8:44 am
Location: spain
Contact:

Post by cecilio » Tue Apr 15, 2008 8:07 pm

Thank you. I don't understand anything about the behaviour of wxString*.

I tried

Code: Select all

delete m_Labels[i];
but it also fails with the same error.

I don't understand why it is failing.

Also, to use the stored strings I have to do

Code: Select all

wxString sText = (*it)->GetData();
and use &sText instead of using (*it) directly. Why this behavior? Is not (*it) a pointer to wxString?

Thank you.

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Tue Apr 15, 2008 8:53 pm

Why do you store Stringpointers at all?

A std::vector<wxString> would do the same.

S.Volkenandt
Knows some wx things
Knows some wx things
Posts: 26
Joined: Tue Nov 14, 2006 1:51 pm
Location: Duesseldorf, Germany

Post by S.Volkenandt » Wed Apr 16, 2008 7:49 am

I suppose you double-delete those pointers, or the pointers stored in the vector are not allocated via new wxString.

cecilio
I live to help wx-kind
I live to help wx-kind
Posts: 189
Joined: Fri Dec 03, 2004 8:44 am
Location: spain
Contact:

Post by cecilio » Wed Apr 16, 2008 2:25 pm

Thank you for the clues. They help me to focus on the problem. There were nothing wrong with wxString* or std::vector. It was just my fault.

I was storing pointers to temporary wxStrings, no longer existing when trying to use them. And also I cause a double delete of the strings: :oops:

Code: Select all

lmDlg dlg = new lmDlg(_("String"));
dlg->ShowModal();


lmDlg::lmDlg(const wxString& sText)
{
    m_Labels.push_back(sText);
    ...
}

lmDlg::~lmDlg()
{
    std::vector<wxString*>::iterator it;
    
    for (it = m_Labels.begin(); it != m_Labels.end(); ++it)
        delete *it;
    m_Labels.clear();
}


Thank you.

eranif
Moderator
Moderator
Posts: 607
Joined: Tue Nov 29, 2005 7:10 pm
Location: Israel

Post by eranif » Wed Apr 16, 2008 5:21 pm

FYI,
You can always use wxArrayString

Eran
IDE: CodeLite + wxCrafter
OS: All
https://wxcrafter.codelite.org
https://codelite.org

cecilio
I live to help wx-kind
I live to help wx-kind
Posts: 189
Joined: Fri Dec 03, 2004 8:44 am
Location: spain
Contact:

Post by cecilio » Wed Apr 16, 2008 8:16 pm

You can always use wxArrayString
Yes, but I'm trying to avoid wxContainers when STL alternatives exist. Clearer code and more portable!

Regards,

Post Reply