Fixed memory issues

Fixed all warnings found by the static analyzer.