0

Having assertion failure while deleting an element from a vector.What are the causes of assertion failure?

5
Contributors
10
Replies
11
Views
5 Years
Discussion Span
Last Post by sita12345
0

Lots of different causes, one being your program may have scribbled something all over memory and damaged vector's memory space. Another problem could be bad use of pointers. Without knowing more details about the program its not possible to give you any really good answers to your question.

One way to debug the program is to start commenting out sections of the code until the assertion failure goes away. That will let you narrow down the problem.

Edited by Ancient Dragon: n/a

0

Hello Thankyou.Below is my code.In the x.erase(iter) i am getting that problem

        vector<x*>::iterator iter;
    for(iter=x.begin();iter!=x.end();++iter)
    {
        delete *iter;
        x.erase(iter);
    }

I debugged but couldn't understand what is the exact problem?

Edited by Nick Evan: Fixed formatting

0

Are you trying to delete a vector of pointers?

std::vector<x*>::iterator begin = x.begin();

while(!x.empty()){
 x* tmp = x.back();
 x.pop_back();
 delete tmp;
}
x.clear();
0

There is a problem with this code:

vector<x*>::iterator iter;
 for(iter=x.begin();iter!=x.end();++iter)
 {
     delete *iter;
     x.erase(iter); // once the element is erased, the iterator is no longer valid
     // ++iter now will cause undefined behaviour. 
 }

Instead, you could use the value returned by erase:

vector<x*>::iterator iter;
 for( iter=x.begin() ; iter!=x.end() ; iter = x.erase(iter) )
 {
     delete *iter;
 }

This is perhaps the simplest way to do it:

for( auto ptr : x ) delete ptr ;
x.clear() ;

Edited by vijayan121: n/a

0

Hi,
I dont want the entire vector to be cleared.I need only few elements to be deleted from a vector.By doing clear all my elements are getting erased?
Vijayan i tried ur code even then it is throwing assertion failure.

0

Make sure what you are deleting is something that should be deleted!. Anyways, the key point to take is that vector::erase returns a iterator pointing to the next element after the one deleted. If possible, put up the whole context(i.e full code )

Edited by firstPerson: n/a

0

Among other things, you may have NULL pointers in your vector, which can't be delete d. Verify what's in your vector before you worry about what's wrong with your deletion code, e.g.:

// changed to while() loop to make logic more obvious
vector<T*>::iterator iter = x.begin();
while (iter != x.end()) {
    T* ptr = *iter;
    if (! ptr) {
        // NULL pointer in your vector
    }
    else if ( /* test to see if element should be removed */ ) {
        delete ptr;
        iter = x.erase(iter);
    }
    else
        iter++;
}
0

Among other things, you may have NULL pointers in your vector, which can't be delete d. Verify what's in your vector before you worry about what's wrong with your deletion code, e.g.:

// changed to while() loop to make logic more obvious
vector<T*>::iterator iter = x.begin();
while (iter != x.end()) {
    T* ptr = *iter;
    if (! ptr) {
        // NULL pointer in your vector
    }
    else if ( /* test to see if element should be removed */ ) {
        delete ptr;
        iter = x.erase(iter);
    }
    else
        iter++;
}

its perfectly fine to delete a null pointer

0

its perfectly fine to delete a null pointer

Lucky me! This is what I get for starting with C programming, and then learning C++ later ... I'm way too careful about handling pointers! And yes, I'm old enough to pre-date a usable C++ compiler.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.