From: Jaroslav Kysela Date: Tue, 15 Aug 2017 15:08:39 +0000 (+0200) Subject: comet: websocket - fix another memory leak and add memory info, fixes #4527 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e489a4adb4beef0c574a2198d0faf052193e1258;p=thirdparty%2Ftvheadend.git comet: websocket - fix another memory leak and add memory info, fixes #4527 --- diff --git a/src/webui/comet.c b/src/webui/comet.c index c30e1f838..107f2bd93 100644 --- a/src/webui/comet.c +++ b/src/webui/comet.c @@ -37,6 +37,7 @@ #include "access.h" #include "notify.h" #include "tcp.h" +#include "memoryinfo.h" static pthread_mutex_t comet_mutex = PTHREAD_MUTEX_INITIALIZER; static tvh_cond_t comet_cond; @@ -54,6 +55,10 @@ static LIST_HEAD(, comet_mailbox) mailboxes; int mailbox_tally; int comet_running; +static memoryinfo_t comet_memoryinfo = { + .my_name = "Comet", +}; + typedef struct comet_mailbox { char *cmb_boxid; /* SHA-1 hash */ char *cmb_lang; /* UI language */ @@ -79,6 +84,11 @@ cmb_destroy(comet_mailbox_t *cmb) LIST_REMOVE(cmb, cmb_link); + memoryinfo_free(&comet_memoryinfo, + sizeof(*cmb) + + (strlen(cmb->cmb_boxid) + 1) + + (cmb->cmb_lang ? strlen(cmb->cmb_lang) + 1 : 0)); + free(cmb->cmb_lang); free(cmb->cmb_boxid); free(cmb); @@ -97,6 +107,7 @@ comet_flush(void) for(cmb = LIST_FIRST(&mailboxes); cmb != NULL; cmb = next) { next = LIST_NEXT(cmb, cmb_link); + printf("refcount %d boxid %s last_used %ld mclk %ld\n", cmb->cmb_refcount, cmb->cmb_boxid, mono2sec(cmb->cmb_last_used), mono2sec(mclk())); if(cmb->cmb_refcount == 1 && cmb->cmb_last_used && cmb->cmb_last_used + sec2mono(60) < mclk()) cmb_destroy(cmb); @@ -138,6 +149,10 @@ comet_mailbox_create(const char *lang) mailbox_tally++; LIST_INSERT_HEAD(&mailboxes, cmb, cmb_link); + + memoryinfo_alloc(&comet_memoryinfo, sizeof(*cmb) + + (strlen(id) + 1) + + (lang ? strlen(lang) + 1 : 0)); return cmb; } @@ -442,6 +457,7 @@ comet_mailbox_ws(http_connection_t *hc, const char *remain, void *opaque) pthread_mutex_lock(&comet_mutex); if (atomic_get(&comet_running)) cmb->cmb_refcount--; + cmb->cmb_last_used = mclk(); pthread_mutex_unlock(&comet_mutex); return res; @@ -455,6 +471,7 @@ comet_init(void) { http_path_t *hp; + memoryinfo_register(&comet_memoryinfo); pthread_mutex_lock(&comet_mutex); tvh_cond_init(&comet_cond); atomic_set(&comet_running, 1); @@ -488,6 +505,9 @@ comet_done(void) tvh_cond_destroy(&comet_cond); + pthread_mutex_lock(&global_lock); + memoryinfo_unregister(&comet_memoryinfo); + pthread_mutex_unlock(&global_lock); } /**