Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2006

Tip: Looking for answers? Try searching our database.

vector iterators invalidated and DEBUG_ITERATORS

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ngaloppo@gmail.com - 19 May 2006 19:15 GMT
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[i])
         return false;
       in.readInt32(indices[i], index_count);
     }

--nico
Sean M. DonCarlos - 19 May 2006 20:46 GMT
> 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
ngaloppo@gmail.com - 19 May 2006 21:46 GMT
> 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
Doug Harrison [MVP] - 19 May 2006 22:48 GMT
>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?

Signature

Doug Harrison
Visual C++ MVP


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.