fix a few memory leaks.
Merge branch 'valgrind' into dev