Fix several memory leaks found by code inspection