Fixing a large number of programming errors found by static analysis

issues found by the coverity scanner, most are memory leaks on error
conditions.