Fix several CID issues

- close some memory/resource leaks
- fix 'usage after free()' issue
- remove empty lines (not CID)