Fix memory errors in various places
This PR fixes a number of memory leaks identified by memcheck and one crash due to zeroed strings reported in the mailing list.
This PR fixes a number of memory leaks identified by memcheck and one crash due to zeroed strings reported in the mailing list.