diff --git a/src/err.c b/src/err.c index 68a0fdde..4f6677ef 100644 --- a/src/err.c +++ b/src/err.c @@ -134,6 +134,31 @@ int err_unlock_mutex(void) { return 0; } +/* + * Let the leak detector know about a chunk of memory + * that needs to be freed, but came from an external library + */ +void err_notify(char *file, int line, void *ptr) { + ERR_LEAK *pnew; + + pnew=(ERR_LEAK*)malloc(sizeof(ERR_LEAK)); + if(!pnew) + log_err(1,"Error: cannot allocate leak struct\n"); + + if(err_lock_mutex()) + log_err(1,"Error: cannot lock error mutex\n"); + + pnew->file=file; + pnew->line=line; + pnew->size=0; + pnew->ptr=ptr; + + pnew->next=err_leak.next; + err_leak.next=pnew; + + err_unlock_mutex(); +} + /* * err_malloc * @@ -199,7 +224,7 @@ void err_free(char *file, int line, void *ptr) { } if(!current) { - log_err(0,"Attempt to free unallocated memory: %s, %d\n",file,line); + log_err(1,"Attempt to free unallocated memory: %s, %d\n",file,line); } else { free(current->ptr); last->next=current->next; diff --git a/src/err.h b/src/err.h index ab6bf2a1..a29921e1 100644 --- a/src/err.h +++ b/src/err.h @@ -48,12 +48,14 @@ extern void log_setdest(char *app, int destination); # define malloc(x) err_malloc(__FILE__,__LINE__,x) # define strdup(x) err_strdup(__FILE__,__LINE__,x) # define free(x) err_free(__FILE__,__LINE__,x) +# define MEMNOTIFY(x) err_notify(__FILE__,__LINE__,x) # endif /* __IN_ERR__ */ extern void *err_malloc(char *file, int line, size_t size); extern char *err_strdup(char *file, int line, const char *str); extern void err_free(char *file, int line, void *ptr); +extern void err_notify(char *file, int line, void *ptr); extern void err_leakcheck(void); #endif /* DEBUG */