Fixed coverity issue 1047568
Fixed coverity issue 1047564
Fixed coverity issue 1047588
Fixed coverity issue 1047592
Fixed coverity issue 1047651
Fixed coverity issue 1047650
Fixed coverity issue 1047649
Fixed coverity issue 1047647
Fixed coverity issue 1047645
Fixed coverity issue 1047567
Fixed various memory leaks and compiler warnings.
Fixed variable mixup.