Fix issues reported by Valgrind.