Fix cppcheck reports:
- (style) Same expression on both sides of '=='
- (style) Same expression on both sides of '||'
- (error) Mismatching allocation and deallocation
- memory leaks
- resource leaks