Sign in to follow this  
Drunken_Monkey

My vector is going screwy on me

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}



?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this