fixed memory leak and illegal memory access