My vector is going screwy on me

Started by
6 comments, last by PlayfulPuppy 17 years, 1 month ago

int j = 0;
for(vector<face*>::iterator i = faces.begin();
	i != faces.end();
	i++, j++)
{
	cout << "deleting " << j << " face\n";
	delete (*(i._Myptr));
}

When I run this loop, the first iteration of this loop runs fine, but the second time around, the program crashes. When I run the debugger, it says "Unhandled exception at 0x104817fd in OBJLoader.exe: 0xC0000005: Access violation writing location 0xfeeefeee." And it points to this line with the arrow in the vector library.

 #if _HAS_ITERATOR_DEBUGGING
			this->_Orphan_all();//<----------- This line here
 #endif /* _HAS_ITERATOR_DEBUGGING */

			_Destroy(_Myfirst, _Mylast);
			this->_Alval.deallocate(_Myfirst, _Myend - _Myfirst);
			}
		_Myfirst = 0, _Mylast = 0, _Myend = 0;
		}

Does anyone have any idea what's going on here?
Advertisement
Why are you refering to the iterator's _Myptr member? Isn't that an implementation detail of the iterator? Never seen that before. Suprised it is public, to be honest.

Normally, you'd just:

    delete *i; *i=0;


for an iterator to a container of pointers.
Oh, I didn't know you could use the delete operator with iterators, thank god for operator overloading, huh?

Well, it still throws the same error and everything.
Quote:Original post by Drunken_Monkey
Oh, I didn't know you could use the delete operator with iterators, thank god for operator overloading, huh?


Sure, but not in this case.

If you have a vector of pointers, you dereference the iterator and then you apply delete to the pointer you get (the pointer is what the iterator points to, and so is what you get when you dereference the iterator).

The error sounds like you're trying to delete a pointer twice.

What happens with:

for (vector<face*>::iterator i = faces.begin(), int j = 0; i != faces.end(); ++i, ++j){    cout << "Deleting " << j << " face \n";    delete *i;}


?
[TheUnbeliever]
Quote:Original post by TheUnbeliever
Quote:Original post by Drunken_Monkey
Oh, I didn't know you could use the delete operator with iterators, thank god for operator overloading, huh?


Sure, but not in this case.

If you have a vector of pointers, you dereference the iterator and then you apply delete to the pointer you get (the pointer is what the iterator points to, and so is what you get when you dereference the iterator).

The error sounds like you're trying to delete a pointer twice.

What happens with:

*** Source Snippet Removed ***

?


It's just doing the exact same thing, just written differently.
That's what I thought/hoped would happen. So in that case, there's nothing wrong with your loop.

The error is that you're deleting the memory more than once however.
- have you got anything like this elsewhere?
[TheUnbeliever]
The faces object is a struct that contains two vectors that contain pointers. There are also two more vectors that point to the exact same thing that faces contains, but it doesn't matter if I delete the struct or the other two vectors first, the error occurs in the same place.

EDIT: Even if I empty the vectors in faces before I delete the object, the same exact thing still occurs.
I'll probably bet good money you've got the same pointer in the list twice, or one of the pointers has been deleted elsewhere.

Try doing this to test it:

for(vector<face*>::iterator i = faces.begin(); i != faces.end(); i++, j++){	for(vector<face*>::iterator ti = faces.begin(); ti != faces.end(); ti++)	{		if(*ti == *i)			cout << "Face " << j << " is in the list twice!\n";			}}


If you don't get any hits with that, just make sure you're not deleting the face elsewhere in the code (Try placing a breakpoint in the destructor).

This topic is closed to new replies.

Advertisement