From f52fe6f1d4811f3a54899d0ad757b170d999f721 Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Wed, 31 Oct 2012 08:52:47 +1200 Subject: [PATCH] Fix non-termination problem in embedded mode (win32). Embedded mode starts a background thread which, under Windows, prevents the process from exiting on a return from main(). We explicitly terminate the process instead of returning. --- ui/main.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/ui/main.c b/ui/main.c index 65551fd..29f208f 100644 --- a/ui/main.c +++ b/ui/main.c @@ -386,22 +386,24 @@ int main(int argc, char **argv) args.devarg.vcc_mv = 3000; args.devarg.requested_serial = NULL; if (parse_cmdline_args(argc, argv, &args) < 0) - return -1; + goto fail_parse; if (args.flags & OPT_EMBEDDED) input_module = &input_async; if (input_module->init() < 0) - return -1; + goto fail_input; output_set_embedded(args.flags & OPT_EMBEDDED); - if (sockets_init() < 0) - return -1; + if (sockets_init() < 0) { + ret = -1; + goto fail_sockets; + } printc_dbg("%s\n", version_text); if (setup_driver(&args) < 0) { - sockets_exit(); - return -1; + ret = -1; + goto fail_driver; } if (device_probe_id(device_default) < 0) @@ -425,10 +427,21 @@ int main(int argc, char **argv) } simio_exit(); - stab_exit(); device_destroy(); + stab_exit(); +fail_driver: sockets_exit(); +fail_sockets: input_module->exit(); +fail_input: +fail_parse: + /* We need to do this on Windows, because in embedded mode we + * may still have a running background thread for input. If so, + * returning from main() won't cause the process to terminate. + */ +#if defined(__Windows__) || defined(__CYGWIN__) + ExitProcess(ret); +#endif return ret; }