More issues reported by Coverity

An assortment of fixes for memory leaks and null pointer dereferences.