Fix "Unexpected null receiver" errors when the program is closed