Fix a number of resource leaks reported by Coverity