Fix covertity reported issues
Fix resource leaks