Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6794227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:11:46+00:00 2026-05-26T18:11:46+00:00

When I am iterating through m_itFileBuffer stringlist container, I get an exception while fetching

  • 0

When I am iterating through “m_itFileBuffer” stringlist container, I get an exception while fetching the value from the iterator.This line of code works most of the times but only some time it gives exception.In my code I am setting “m_itFileBuffer” iterator to different values. the part of the code is given below

StringList m_listFileBuffer; //this contains list of CString`s, I read from file and insert into this.
StringList::iterator m_itFileBuffer;
....
....
....
....
....
{
    bool notEmpty = (m_itFileBuffer != m_MylistFileBuffer.end());

    if (notEmpty)
    {

        m_strLine = static_cast<CString>(*m_itFileBuffer);//Here i get exception 

        ++m_itFileBuffer;
    }
}

Below is exception which i get in output window:

Severity: Critical Error (10 - 'System Crit.'), Returncode: 0x80040835, Error No.: 0 (access violation)
Description: C system exception code: C0000005

Any help, why i am getting this exception?
Also, How can we reset the iterator?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:11:47+00:00Added an answer on May 26, 2026 at 6:11 pm

    I assume StringList is actually:

    typedef std::list<CString> StringList;
    

    Maybe you should consider using std::string instead of CString.

    Now coming to the iteration through the list. The code you use to iterate looks strange to me. It would be simpler to do it like this:

    for (StringList::/*const_*/iterator it=m_listFileBuffer.begin(); it != m_listFileBuffer.end(); ++it)
    {
       /*const*/ CString& strLine = *it; //no need for static cast
       std::cout << (LPCTSTR) strLine << std::endl;
    }
    

    If you have Visual Studio 2010 (contains some of the C+11 stuff implemented) you can write the loop more concisely with auto:

    for (auto it = begin(m_listFileBuffer); it != end(m_listFileBuffer); ++it)
    {
       /*const*/ CString& strLine = *it; //no need for static cast
       std::cout << (LPCTSTR) strLine << std::endl;
    }
    

    EDITED by sehe:

    With full C++11 support, simply write

    for (/*const*/ auto& strLine : m_listFileBuffer)
        std::cout << (LPCTSTR) strLine << std::endl; 
    

    EDITED by ds27680:

    Please see also the comments…

    To answer also your question related to the crash, this can happen due to various reasons. I will enumerate some of the most obvious ones:

    1. The iterator is initialized with the begin of one list but checked on the end of another list. comparing iterators from different containers is a bad idea
    2. You are doing operations (in the same or another thread) on the list that invalidate the iterator but you are using it afterwards. I.e. m_listFileBuffer.erase(it);
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is iterating through the vector using an iterator and copying to a list the
I am iterating through a file and on each line I am looking for
Duplicate Modifying A Collection While Iterating Through It Has anyone a nice pattern to
I'm iterating through a collection of asp:tablerows to be able to get or set
So I'm iterating through all the checked items in the box like this: for
Need help with exception handling for arrayObject. I'm iterating through a series of objects
I'm totally confused as of why this is not working? I'm iterating through a
While iterating through an object containing data I'm checking if the url of image
I'm iterating through this object: var object = { first : { child1 :
I'm iterating through my controls on this web page and when a button is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.