[pacman-dev] [PATCH] Fix memleaks in front-end caused by dynamic memory allocation

Aaron Griffin aaronmgriffin at gmail.com
Sat Oct 25 01:49:31 EDT 2008


On Fri, Oct 24, 2008 at 4:03 PM, Dan McGee <dpmcgee at gmail.com> wrote:
> Can we just expose something more like "free_conflictlist"? Having
> those casts and stuff is rather ugly on the public side of the library
> usage.

I was always a fan of the libraries that did something of the sort:

struct list_node {
   void *data;
   struct list_node *next;
   void (*free_func)(void *);
};
...
void free_list_node(struct list_node *node) {
   if(node->free_func && node->data) {
      node->free_func(node->data);
      free(node);
   }
}

And free_func is set when a node is added. Alternatively:

struct list_node {
   void *data;
   struct list_node *next;
}
struct list_head {
   struct list_node *head;
   void (*free_func)(void *);
}
...
void free_list(struct list_head *l) {
   void (*fn)(void *) = l->free_func;
   for(struct list_node i = l->head; i;) {
      if(fn && i->data) fn(i->data);
      struct list_node *tmp = i->next;
      free(i);
      i = tmp;
   }
}

I know it's a lot of work, but it's far cleaner than exposing node
freeing functions to the front ends.



More information about the pacman-dev mailing list