fix memory leak bugs