• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

Iterator is dead

I am using the following for loop to iterate through STL list, but I get a runtime "Access Violation" on the cornerLstIt++ statement.  It seems as though the iterator is dead by the time it gets through the loop once (Everything works fine up until the point of the error) What is happening to my iterator?

for(cornerLstIt = cornerLst.begin(); cornerLstIt != cornerLst.end() ;cornerLstIt++;

                strcpy(filepath, (*cornerLstIt)->bitmapFile);
            //need to put Corner\\ in front.
            strcpy(filepath, strcat(filepath, append));
            strcpy(filepath, strcat(directory, filepath));
                  SetTempBitmapFromFile(filepath, tempBitmap);
            curImageList->Add(tempBitmap, tempBitmap);
1 Solution
There should not be a semicolon (;) at the end of cornerLstIt++. You will get a compiler error, I think. I don't think the Access Violation happens on the cornerLstIt++. Most likely, it happens on strcpy. Is the filepath buffer large enough?
HVDCAuthor Commented:
The (;) is a typo--that is not the problem.  I was stepping through the function, the error actually occurs inside the operator++ function for the iterator
It doesn't lokk like it's the iterator's fault. Something else must be going on, probably someone is stepping on memory used by the iterator before the cornerLstIt++ call.

Some comments:
1. Use std:string instead of strcpy and strcat. This way there is no danger of overflows (and the syntax in much nicer).
2. Prefer ++it to it++, since it++ creates a temporary object that you are not using (and there's a good chance it is not optimized out).
3. Why the line:
    strcpy(filepath, strcat(filepath, append));
    And not just:
    strcat(filepath, append);
    The strcpy just copies filepath to itself.
4. Is the line
    curImageList->Add(tempBitmap, tempBitmap);
    another typo? Why do you need to pass the same parameter twice?
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

>> strcpy(filepath, strcat(filepath, append));

will become:

strcat(filepath, append);
strcopy(filepath, filepath);

as yonat said, only the strcat is needed, the strcpy is useless (and looks suspicious).

It could be memory problem (especially cause u
using strcpy ...).

One more point. STL has bugs when
passing STL container between DLLs/Library.
Casue they are using static variable and think that this variable
is one in applicatin.

Check your code without library and DLLs.
HVDCAuthor Commented:
It turns out that the error was in the string manipulations--but I still don't understand how the error would come up in a function that is unrelated....Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now