Fix resource leaks found by coverity