Fix iterator invalidation. PR16935.
Test case for PR16933/r188707.