Fixed memory leaks found by Coverity Scan.
More info: https://scan.coverity.com/projects/2787