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);
            
  }
HVDCAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
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?
0
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
0
yonatCommented:
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?
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

KangaRooCommented:
>> 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).
0
ronenhhCommented:
Hi

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.