fixed some mem leaks
authorBrenden Matthews <brenden1@rty.ca>
Thu, 10 Nov 2005 04:19:43 +0000 (04:19 +0000)
committerBrenden Matthews <brenden1@rty.ca>
Thu, 10 Nov 2005 04:19:43 +0000 (04:19 +0000)
git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky@387 7f574dfc-610e-0410-a909-a81674777703

ChangeLog
src/conky.c
src/conky.h
src/linux.c
src/top.c

index 9a64381..3cee128 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,7 @@
 
 2005-11-09
        * Cleaned up top code, bug fixes
+       * Fixed some memory leaks
 
 2005-11-08
        * Fix bug# 1351686.  Patch by boojit.
index f6d6d97..bbcad04 100644 (file)
@@ -1012,6 +1012,8 @@ static void free_text_objects()
                        close(text_objects[i].data.i2c.fd);
                        break;
                case OBJ_time:
+                       free(text_objects[i].data.s);
+                       break;
                case OBJ_utime:
                case OBJ_if_existing:
                case OBJ_if_mounted:
@@ -1064,6 +1066,18 @@ static void free_text_objects()
                        free(text_objects[i].data.execi.cmd);
                        free(text_objects[i].data.execi.buffer);
                        break;
+               case OBJ_top:
+                       if (info.first_process) {
+                               free_all_processes(info.first_process);
+                               info.first_process = NULL;
+                       }
+                       break;
+               case OBJ_top_mem:
+                       if (info.first_process) {
+                               free_all_processes(info.first_process);
+                               info.first_process = NULL;
+                       }
+                       break;
                }
        }
 
@@ -4271,6 +4285,10 @@ static void main_loop()
                        {
                        ERR("received SIGINT or SIGTERM to terminate. bye!");
                        clean_up();
+#ifdef X11
+                       XDestroyRegion(region);
+                       region = NULL;
+#endif /* X11 */
                        return;  /* return from main_loop */
                        /*break;*/
                        }
@@ -4287,6 +4305,10 @@ static void main_loop()
                g_signal_pending=0;
        
        }
+#ifdef X11
+       XDestroyRegion(region);
+       region = NULL;
+#endif /* X11 */
 }
 
 static void load_config_file(const char *);
index f8b6356..fa766b5 100644 (file)
@@ -193,6 +193,7 @@ struct information {
 #endif
        struct process *cpu[10];
        struct process *memu[10];
+       struct process *first_process;
        unsigned long looped;
 #ifdef TCP_PORT_MONITOR
         tcp_port_monitor_collection_t * p_tcp_port_monitor_collection;
@@ -354,6 +355,8 @@ struct process {
 };
 
 void update_top();
+void free_all_processes();
+struct process *get_first_process();
 
 /* fs-stuff is possibly system dependant (in fs.c) */
 
index ba04a39..fb2fc70 100644 (file)
@@ -1172,6 +1172,7 @@ void update_top()
 {
        show_nice_processes = 1;
        process_find_top(info.cpu, info.memu);
+       info.first_process = get_first_process();
 }
 
 
index 7a72ab8..7899671 100644 (file)
--- a/src/top.c
+++ b/src/top.c
@@ -13,7 +13,23 @@ static unsigned long g_time = 0;
 static unsigned long previous_total = 0;
 static struct process *first_process = 0;
 
+struct process *get_first_process()
+{
+       return first_process;
+}
 
+void free_all_processes(struct process *pr)
+{
+       struct process *next = NULL;
+       while (pr) {
+               next = pr->next;
+               if (pr->name) {
+                       free(pr->name);
+               }
+               free(pr);
+               pr = next;
+       }
+}
 
 static struct process *find_process(pid_t pid)
 {
@@ -163,8 +179,9 @@ static int process_parse_stat(struct process *process)
                *q = 0;
        }
 
-       if (process->name)
+       if (process->name) {
                free(process->name);
+       }
        process->name = strdup(deparenthesised_name);
        process->rss *= getpagesize();
 
@@ -318,8 +335,9 @@ static void delete_process(struct process *p)
        else
                first_process = p->next;
 
-       if (p->name)
+       if (p->name) {
                free(p->name);
+       }
        free(p);
 }
 
@@ -428,14 +446,14 @@ int insert_sp_element(
                        sp_readthru->greater=sp_cur;
                        did_insert = ++x;  /* element was inserted, so increase the counter */
                }
-       }  
+       }
        if (x < max_elements && sp_readthru == NULL && !did_insert) {
                /* sp_cur is the smallest element and list isn't full, so insert at the end */  
                (*p_sp_tail)->less=sp_cur;
                sp_cur->greater=*p_sp_tail;
                *p_sp_tail = sp_cur;
                did_insert=x;
-       } else if (x==max_elements && sp_readthru != NULL) {
+       } else if (x == max_elements && sp_readthru != NULL) {
                /* we inserted an element and now the list is too big by one. Destroy the smallest element */
                sp_destroy = sp_readthru;
                sp_readthru->greater->less = NULL;
@@ -464,16 +482,16 @@ struct sorted_process * malloc_sp(struct process * proc) {
 /*
  * copy the procs in the sorted list to the array, and destroy the list 
  */
-void sp_acopy(struct sorted_process *sp_head, struct process ** ar, int max_size) {
-
+void sp_acopy(struct sorted_process *sp_head, struct process ** ar, int max_size)
+{
        struct sorted_process * sp_cur, * sp_tmp;
        int x;
        sp_cur = sp_head;
-       for (x=0; x < max_size && sp_cur != NULL; x++) {
+       for (x = 0; x < max_size && sp_cur != NULL; x++) {
                ar[x] = sp_cur->proc;   
                sp_tmp = sp_cur;
-               sp_cur= sp_cur->less;
-               free(sp_tmp);   
+               sp_cur = sp_cur->less;
+               free(sp_tmp);
        }
 }
 
@@ -484,10 +502,10 @@ void sp_acopy(struct sorted_process *sp_head, struct process ** ar, int max_size
 
 inline void process_find_top(struct process **cpu, struct process **mem)
 {
-       struct sorted_process *spc_head=NULL, *spc_tail=NULL, *spc_cur=NULL;
-       struct sorted_process *spm_head=NULL, *spm_tail=NULL, *spm_cur=NULL;
-       struct process *cur_proc=NULL;
-       unsigned long total =0;
+       struct sorted_process *spc_head = NULL, *spc_tail = NULL, *spc_cur = NULL;
+       struct sorted_process *spm_head = NULL, *spm_tail = NULL, *spm_cur = NULL;
+       struct process *cur_proc = NULL;
+       unsigned long total = 0;
 
        if (!top_cpu && !top_mem) return;
 
@@ -499,16 +517,17 @@ inline void process_find_top(struct process **cpu, struct process **mem)
        cur_proc = first_process;
 
        while (cur_proc !=NULL) {
-               if (top_cpu) { 
+               if (top_cpu) {
                        spc_cur = malloc_sp(cur_proc);
-                       insert_sp_element(spc_cur, &spc_head, &spc_tail, MAX_SP, &compare_cpu); 
-               } 
+                       insert_sp_element(spc_cur, &spc_head, &spc_tail, MAX_SP, &compare_cpu);
+               }
                if (top_mem) {
                        spm_cur = malloc_sp(cur_proc);
-                       insert_sp_element(spm_cur, &spm_head, &spm_tail, MAX_SP, &compare_mem); 
+                       insert_sp_element(spm_cur, &spm_head, &spm_tail, MAX_SP, &compare_mem);
                }
                cur_proc = cur_proc->next;
        }
        sp_acopy(spc_head, cpu, MAX_SP);
        sp_acopy(spm_head, mem, MAX_SP);
-} 
+}
+