Try to prevent memory corruption when closing MainWindow