Weird crasher with std::remove, std::erase

G

groups

This small piece of code is troubling me.. is there anything wrong
with it??
After calling this method the contents ot the input vector are
completely screwed..
(CCountedCIG_CountZero() applies only to a few elements in the vector)

void UElementVector::ClearZeroCountElements(vector<CCountedCIG*>
&ioItemVec)
{
vector<CCountedCIG*>::iterator new_end = remove_if(ioItemVec.begin(),
ioItemVec.end(), CCountedCIG_CountZero());

for (vector<CCountedCIG*>::iterator it = new_end; it !=
ioItemVec.end(); it++)
{
delete *it;
*it = NULL;
}

ioItemVec.erase(new_end, ioItemVec.end());
}


Any help is much apreciated!

regards,
jan
 
H

Howard Hinnant

This small piece of code is troubling me.. is there anything wrong
with it??
After calling this method the contents ot the input vector are
completely screwed..
(CCountedCIG_CountZero() applies only to a few elements in the vector)

void UElementVector::ClearZeroCountElements(vector<CCountedCIG*>
&ioItemVec)
{
vector<CCountedCIG*>::iterator new_end = remove_if(ioItemVec.begin(),
ioItemVec.end(), CCountedCIG_CountZero());

for (vector<CCountedCIG*>::iterator it = new_end; it !=
ioItemVec.end(); it++)
{
delete *it;
*it = NULL;
}

ioItemVec.erase(new_end, ioItemVec.end());
}

<nod> The problem is that remove_if works just by overwriting elements
that no longer belong in the sequence. It doesn't swap them to the end.
What is most likely happening is that you are overwriting the pointers
you want to delete. And then you are deleting copies of the pointers
you want to keep. A subsequent dereference leads to a crash.

You could do a two-pass operation where the first pass did:

1. Check if you want to delete, and if so:
2. delete
3. null pointer

transform could possibly be employed for the first pass.

And then the second pass could call remove (if null pointer).

Or you could write your own algorithm which combines these passes into a
single pass.

-Howard
 
G

groups

<nod> The problem is that remove_if works just by overwriting elements
that no longer belong in the sequence. It doesn't swap them to the end.
What is most likely happening is that you are overwriting the pointers
you want to delete. And then you are deleting copies of the pointers
you want to keep. A subsequent dereference leads to a crash.


D'oh! Thanks for the hint -- carefully re-reading the docs it's all
there..

| Remove_if removes from the range [first, last) every element x such
that pred(x) is true.
| That is, remove_if returns an iterator new_last such that the range
[first, new_last)
| contains no elements for which pred is true. [1] The iterators in
the range [new_last, last)
| are all still dereferenceable, but the elements that they point to
are unspecified.
| ..


cheers,
jan
 
T

terminator

<nod> The problem is that remove_if works just by overwriting elements
that no longer belong in the sequence. It doesn't swap them to the end.
What is most likely happening is that you are overwriting the pointers
you want to delete. And then you are deleting copies of the pointers
you want to keep. A subsequent dereference leads to a crash.

D'oh! Thanks for the hint -- carefully re-reading the docs it's all
there..

| Remove_if removes from the range [first, last) every element x such
that pred(x) is true.
| That is, remove_if returns an iterator new_last such that the range
[first, new_last)
| contains no elements for which pred is true. [1] The iterators in
the range [new_last, last)
| are all still dereferenceable, but the elements that they point to
are unspecified.
| ..

cheers,
jan

This behavior surprises me.I have read the source code for remove
family but I do not understand how they can be useful!!!????
remembering that they won`t work with map whose elements are none-
assignable 'pair's.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top