Fixed memory errors caught by llvm AddressSanitizer