Fix a crash with invalid iterators