The number of iterations is limited to the initial array size, but we still need to check if the array did shrink since then before accessing each element. Fixes #1992.