valgrind fixes

Reviewer: @poolakiran

These changes don't actually fix any memory leaks, but they do satisfy valgrind
so that new leaks will be easier to spot.