Fix potential invalid iterator bug
Merge pull request #1225 from dorkster/coverity

Fix potential invalid iterator bug