diff --git a/src/nhttpd/yhttpd.cpp b/src/nhttpd/yhttpd.cpp index 5cc3f8115..054971bf6 100644 --- a/src/nhttpd/yhttpd.cpp +++ b/src/nhttpd/yhttpd.cpp @@ -104,6 +104,15 @@ void yhttpd_reload_config() { //----------------------------------------------------------------------------- // Main Entry //----------------------------------------------------------------------------- + +void thread_cleanup (void *p) +{ + Cyhttpd *y = (Cyhttpd *)p; + if (y) + delete y; + y = NULL; +} + #ifndef Y_CONFIG_BUILD_AS_DAEMON void * nhttpd_main_thread(void *) { pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0); @@ -116,6 +125,8 @@ void * nhttpd_main_thread(void *) { aprintf("Error initializing WebServer\n"); return (void *) EXIT_FAILURE; } + /* we pthread_cancel this thread from the main thread, but still want to clean up */ + pthread_cleanup_push(thread_cleanup, yhttpd); yhttpd->flag_threading_off = true; yhttpd->hooks_attach(); @@ -127,7 +138,9 @@ void * nhttpd_main_thread(void *) { yhttpd->run(); } + pthread_cleanup_pop(0); delete yhttpd; + yhttpd = NULL; aprintf("Main end\n"); return (void *) EXIT_SUCCESS; @@ -233,8 +246,10 @@ Cyhttpd::Cyhttpd() { } //----------------------------------------------------------------------------- Cyhttpd::~Cyhttpd() { + stop_webserver(); if (webserver) delete webserver; + CLanguage::deleteInstance(); webserver = NULL; }