]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
comet: websocket - fix another memory leak and add memory info, fixes #4527
authorJaroslav Kysela <perex@perex.cz>
Tue, 15 Aug 2017 15:08:39 +0000 (17:08 +0200)
committerJaroslav Kysela <perex@perex.cz>
Tue, 15 Aug 2017 15:08:39 +0000 (17:08 +0200)
src/webui/comet.c

index c30e1f8386765fd627cd98143a028c867803d5ca..107f2bd936de897be03113c2b1af459bd5902b3a 100644 (file)
@@ -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);
 }
 
 /**