fix memory deleting issues