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?

Mat
  • 202,337
  • 40
  • 393
  • 406
Nikhil
  • 299
  • 3
  • 6
  • 17
  • How are you initializing `m_itFileBuffer`? – Mat Nov 11 '11 at 10:52
  • 1
    //initialize the begin of file m_itFileBuffer = m_listFileBuffer.begin(); – Nikhil Nov 11 '11 at 11:05
  • How come you have m_listFileBuffer and m_MylistFileBuffer? – Mat Nov 11 '11 at 11:11
  • posted incorrectly ..it is m_listFileBuffer only :) – Nikhil Nov 11 '11 at 11:16
  • 1
    Well please post your real code, and all the relevant parts of it (the initialization is important). – Mat Nov 11 '11 at 11:17
  • And delete useless comments after editing the post :) – sehe Nov 11 '11 at 11:26
  • FYI that's not an exception in the C++ sense (but the OS calls its own gubbins an "exception" too). And can you post your _testcase_? Y'know, the _minimal_, _complete_ sample program that you have been using in your debugging of this issue. The one that's about 20 lines long at most, and that compiles, and that exhibits the issue. – Lightness Races in Orbit Nov 11 '11 at 11:32

1 Answers1

1

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);
ds27680
  • 1,993
  • 10
  • 11
  • @sehe Well considering he uses CString he probably uses visual Studio. As far as I know ranged for is not present in VS 2010, and even not in the C+11 features implemented currently in VS 2011. So my comment related to your edit would be "nice" but it is probably going to stay unavailable for at least one year if not longer... – ds27680 Nov 11 '11 at 11:45
  • That's sad to hear. I guess I didn't want to assume MSVC++ (others read the answers too). Amazing that range-based-for would be out of scope for a major compiler vendor. – sehe Nov 11 '11 at 11:47
  • @sehe. See http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx. Not clear for me how a way to write the iteration that is not supported at this time (probably only gcc has it implemented (since v4.6) and I doubt he is using gcc) will practically help Nikhil to solve his problem. This is however how he will be able to write it in 1 or 2 years from now ( I hope :-) ) – ds27680 Nov 11 '11 at 11:53
  • A way to write it, today is [`for each (T& v in array)`](http://stackoverflow.com/questions/6898859/does-msvc10-visual-studio-2010-support-c-range-based-loops) – sehe Nov 11 '11 at 11:56
  • @sehe Not certain but as far as I know this synthax is indeed implemented based on a proposal for ranged for. I expect however that this form did not make it in the final standard. So if you start writing code like that you will probably have more than one problem in the future... But maybe that's me. I wouldn't use it and risk that sometime in the future i will have to replace all occurences in the code with the standard and portable form. – ds27680 Nov 11 '11 at 12:11