fix memory leaks bugs