fix a memory leak when closing