fixed memory bugs detected by AddressSanitizer