Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Entire forum
➜ Programming
➜ STL
➜ Deconstructors and lists
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Greven
Canada (835 posts) Bio
|
Date
| Thu 28 Jul 2005 03:58 AM (UTC) |
Message
| I have a class that has a deconstructor. In the deconstructor function, I remove it from the list that it is in with this:
OLC_BOUNTY_DATA::~OLC_BOUNTY_DATA()
{
std::list<OLC_BOUNTY_DATA * >::iterator iter;
OLC_BOUNTY_DATA * bounty;
for (iter = olc_bounties.begin(); iter != olc_bounties.end(); iter++ )
{
bounty = (*iter);
if ( bounty == this)
{
olc_bounties.erase(iter);
}
}
}
Can someone tell me what I'm doing wrong? It keeps crashing when iter is dereferenced to be assigned into bounty. |
Nobody ever expects the spanish inquisition!
darkwarriors.net:4848
http://darkwarriors.net | Top |
|
Posted by
| Darrik Vequir
(24 posts) Bio
|
Date
| Reply #1 on Thu 28 Jul 2005 12:45 PM (UTC) |
Message
|
You want to add a 'return' after olc_bounties.erase call. the iterator behaves in an undefined fashion if you erase something from a list but continue iterating through it.
DV | Top |
|
Posted by
| Greven
Canada (835 posts) Bio
|
Date
| Reply #2 on Thu 28 Jul 2005 04:02 PM (UTC) |
Message
| ... How did I miss that? Thanks. |
Nobody ever expects the spanish inquisition!
darkwarriors.net:4848
http://darkwarriors.net | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #3 on Mon 01 Aug 2005 07:03 PM (UTC) |
Message
| Another thing you can do is to iterate over a copy of the list, removing from the original. That's something you'd need to do when you need to remove multiple elements. Of course, there is a whole new set of pitfalls, e.g. the copied list obviously won't have the same iterator pointers as the old list... |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Darrik Vequir
(24 posts) Bio
|
Date
| Reply #4 on Tue 02 Aug 2005 01:54 PM (UTC) |
Message
|
Haven't hit a situation where I can't just it = list.begin() after an erase... or, I should say, haven't had a situation where the list is big enough to matter if it has to reset and re-search 4-5 times.
I usually try to use other containers if I get into those situations.
DV | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #5 on Tue 02 Aug 2005 05:09 PM (UTC) |
Message
| Well, right, you can always just restart the whole iteration when you erase. But as you imply, that's really not very efficient when you have large lists, and even less efficient when comparing elements is a slow operation. What other containers would you have in mind? |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Darrik Vequir
(24 posts) Bio
|
Date
| Reply #6 on Wed 03 Aug 2005 12:54 PM (UTC) |
Message
|
I've mostly used maps for any large list I may need. I can find SOME key to look it up by that avoids having to iterate through the entire list to find something! :).
then again, I've just barely gotten the database working on my scratch base, have the most rudimentary of object structure, so its possible I may have to come up with a better system in the future.
DV | Top |
|
Posted by
| Kiasyn Kelle
(15 posts) Bio
|
Date
| Reply #7 on Sun 25 Sep 2005 10:41 AM (UTC) |
Message
| I think you can use something like:
OLC_BOUNTY_DATA::~OLC_BOUNTY_DATA()
{
std::list<OLC_BOUNTY_DATA * >::iterator iter;
OLC_BOUNTY_DATA * bounty;
for (iter = olc_bounties.begin(); iter != olc_bounties.end(); )
{
bounty = (*iter);
if ( bounty == this)
olc_bounties.erase(iter++);
else
++iter;
}
}
| Top |
|
Posted by
| Samson
USA (683 posts) Bio
|
Date
| Reply #8 on Sun 25 Sep 2005 04:33 PM (UTC) |
Message
|
OLC_BOUNTY_DATA::~OLC_BOUNTY_DATA()
{
std::list<OLC_BOUNTY_DATA * >::iterator iter;
OLC_BOUNTY_DATA * bounty;
for (iter = olc_bounties.begin(); iter != olc_bounties.end(); )
{
bounty = (*iter);
iter++;
if ( bounty == this)
{
olc_bounties.remove(bounty);
}
}
}
You can also do it like so. All of my std::list calls use the remove() member instead of erase() and I set them all up in a similar manner to the above. They all work just fine this way for multiple member removal. | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
28,778 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top