vector iterators invalidated and DEBUG_ITERATORS

N

ngaloppo

Hi,

compiling a program with the code blurb below causes a runtime error
("Expression: vector iterators incompatible") due to the debug
iterators in VC++ 8. The error happens in the ind_len.push_back() call,
at the second iteration. I suspect that the iterator has been
invalidated, but I can't see how this would be from one iteration to
another in this tiny for loop. Could anyone give me more insight?
Thanks!

std::vector<int*> indices;
std::vector<int> ind_len;

[...]

int patch_count = in.readUInt32();
for (int i = 0; i < patch_count; i++)
{
int index_count = in.readUInt32();
ind_len.push_back(index_count);
indices.push_back(new int[index_count]);
if (!indices)
return false;
in.readInt32(indices, index_count);
}

--nico
 
G

Guest

compiling a program with the code blurb below causes a runtime error
("Expression: vector iterators incompatible") due to the debug
iterators in VC++ 8. The error happens in the ind_len.push_back() call,
at the second iteration. I suspect that the iterator has been
invalidated, but I can't see how this would be from one iteration to
another in this tiny for loop. Could anyone give me more insight?

In VC++ 8, iterators into vectors become invalidated whenever the number of
elements in the vector changes. The invalidation happens immediately. The
size of the loop is irrelevant.

You can use #define _HAS_ITERATOR_DEBUGGING 0 to prevent the invalidation,
but I don't recommend it unless you're really sure about what you're doing.

Sean
 
N

ngaloppo

Sean said:
In VC++ 8, iterators into vectors become invalidated whenever the number of
elements in the vector changes. The invalidation happens immediately. The
size of the loop is irrelevant.

You can use #define _HAS_ITERATOR_DEBUGGING 0 to prevent the invalidation,
but I don't recommend it unless you're really sure about what you're doing.

Thanks for your reply. I did know about the work-around, but I'm not
confident enough to simply apply it, especially because I don't know
what is going on here. Anyway, a simple loop pushing data onto the end
of a vector shouldn't be a problem, should it? I understand that
iterators into the vector are being invalidated by the push_back(), but
I'm not holding, nor using iterators into the vector.

--nico
 
D

Doug Harrison [MVP]

Thanks for your reply. I did know about the work-around, but I'm not
confident enough to simply apply it, especially because I don't know
what is going on here. Anyway, a simple loop pushing data onto the end
of a vector shouldn't be a problem, should it?

It will invalidate end(), but unless reallocation happens, it won't
invalidate any other iterator. You can prevent reallocation by using
reserve() beforehand.
I understand that
iterators into the vector are being invalidated by the push_back(), but
I'm not holding, nor using iterators into the vector.

Can you post a small console program that demonstrates the problem?
 

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

Top