fix memory leak in flush function
fixing some memory leak