fix memory leak and crash