Fix issue 366: Crash (uncaught exception) on closing application