Tear down GUI before stopping event loop, this fixes a crash on windows (and perhaps a rare crash on other OS).