Fix leaks with iterators and exceptions

Lots of edge cases. All sorts of order of evaluation issues,
a number of leaks, and at least one over-eager free.