For Programmers: Free Programming Magazines  


Home > Archive > VC STL > March 2006 > std::vector::swap invalidates iterators, per _SECURE_SCL









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author std::vector::swap invalidates iterators, per _SECURE_SCL
andhow

2006-03-26, 10:03 pm

The standard (23.1.11) specifies that swapping two vectors does not
invalidate the iterators, pointers, and references to its elements.
This would lead us to assume the following code is well defined:

#include <vector>
int main()
{
std::vector<int> v1, v2;
std::vector<int>::iterator i = v1.begin();
bool b = i == v1.begin();
v1.swap(v2);
b = i == v2.begin();
}

The new checked STL implementation in VS2005 keeps track of which
container an iterator is iterating over by keeping a pointer to the
container. When _HAS_ITERATOR_DEBUGGING is defined (what you get by
default in a Debug build), you can clearly see the above code is
supported because the swap goes through all the iterators and swaps who
is pointing to who. The problem is that in Release builds, when
_SECURE_SCL is defined to 1, which it is by default, the containers are
also tracked, but the swap operation does not swap container pointers,
so you get a very nasty assertion. I assume this is an omission.

The workaround, btw, is to #define _SECURE_SCL to 0. One would hope
this is temporary.

Carl Daniel [VC++ MVP]

2006-03-26, 10:03 pm

andhow wrote:
> The standard (23.1.11) specifies that swapping two vectors does not
> invalidate the iterators, pointers, and references to its elements.
> This would lead us to assume the following code is well defined:
>
> #include <vector>
> int main()
> {
> std::vector<int> v1, v2;
> std::vector<int>::iterator i = v1.begin();
> bool b = i == v1.begin();
> v1.swap(v2);
> b = i == v2.begin();
> }
>
> The new checked STL implementation in VS2005 keeps track of which
> container an iterator is iterating over by keeping a pointer to the
> container. When _HAS_ITERATOR_DEBUGGING is defined (what you get by
> default in a Debug build), you can clearly see the above code is
> supported because the swap goes through all the iterators and swaps
> who is pointing to who. The problem is that in Release builds, when
> _SECURE_SCL is defined to 1, which it is by default, the containers
> are also tracked, but the swap operation does not swap container
> pointers, so you get a very nasty assertion. I assume this is an
> omission.
>
> The workaround, btw, is to #define _SECURE_SCL to 0. One would hope
> this is temporary.


I'd suggest that you file a bug report at

http://lab.msdn.microsoft.com/productfeedback

Post the link to your bug report here so that others can validate and vote
on it.

-cd


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com