fix bugs reported by running test with valgrind

merge in [MAPREDUCE-6005](https://issues.apache.org/jira/browse/MAPREDUCE-6005)