Fix crash when closing a window with open documents