fixed some crash and memory leaks in abnormal cases