Add memory debugging (--enable-mem-debug), find and fix several small (and not so small) memory leaks

This commit is contained in:
Ron Pedde 2007-01-17 01:06:16 +00:00
parent b23f0d226b
commit 14fe9f9c6c
53 changed files with 627 additions and 364 deletions

View File

@ -6,13 +6,14 @@ AC_INIT(config.h.in)
AM_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE(mt-daapd,0.2.1.1)
AC_USE_SYSTEM_EXTENSIONS
dnl Checks for programs.
AC_PROG_CC
dnl AC_PROG_YACC
dnl AM_PROG_LEX
AC_PROG_LIBTOOL
AC_USE_SYSTEM_EXTENSIONS
AC_CHECK_HEADERS([sys/wait.h])
AC_CHECK_HEADERS([sys/param.h])
@ -115,6 +116,10 @@ AC_ARG_ENABLE(gdbm,[ --enable-gdbm Enable gdbm support],
AC_ARG_ENABLE(ffmpeg,[ --enable-ffmpeg Enable ffmpeg transcode support],
use_ffmpeg=true;)
AC_ARG_ENABLE(mem-debug,[ --enable-mem-debug Enable memory debugging],
CPPFLAGS="${CPPFLAGS} -DDEBUG_MEM")
AM_CONDITIONAL(COND_REND_HOWL, test x$rend_howl = xtrue)
AM_CONDITIONAL(COND_REND_POSIX, test x$rend_posix = xtrue)
AM_CONDITIONAL(COND_REND_AVAHI, test x$rend_avahi = xtrue)

View File

@ -62,6 +62,7 @@ mt_daapd_SOURCES = main.c daapd.h rend.h uici.c uici.h webserver.c \
smart-parser.c smart-parser.h xml-rpc.c xml-rpc.h \
os.h ll.c ll.h conf.c conf.h compat.c compat.h util.c util.h \
os-unix.h os-unix.c os.h plugin.c plugin.h db-sql-updates.c \
memdebug.c memdebug.h \
$(PRENDSRC) $(ORENDSRC) $(HRENDSRC) $(ARENDSRC) $(OGGVORBISSRC) \
$(FLACSRC) $(MUSEPACKSRC) $(SQLITEDB) $(SQLITE3DB) $(SQLDB) $(GDBM)

View File

@ -8,6 +8,8 @@
#include <string.h>
#include <stdio.h>
#include "daapd.h"
#if !HAVE_STRCASESTR
/* case-independent string matching, similar to strstr but
* matching */

View File

@ -48,10 +48,10 @@
#include <sys/stat.h>
#include <sys/types.h>
#include "daapd.h"
#include "conf.h"
#include "err.h"
#include "ll.h"
#include "daapd.h"
#include "os.h"
#include "util.h"
#include "webserver.h"
@ -1696,5 +1696,6 @@ char *conf_get_servername(void) {
retval[MAX_REND_LEN-3] = '.';
}
free(template);
return retval;
}

View File

@ -8,6 +8,7 @@
#define os_syslog(a,b)
#define os_closesyslog()
#include "daapd.h"
#include "conf.h"
#include "err.h"

View File

@ -50,6 +50,7 @@
# include <sys/wait.h>
#endif
#include "daapd.h"
#include "conf.h"
#include "configfile.h"
#include "db-generic.h"

View File

@ -31,7 +31,16 @@
#ifndef _DAAPD_H_
#define _DAAPD_H_
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
#include <time.h>
#include "memdebug.h" /* redefine free/malloc/etc */
#include "compat.h"
#include "webserver.h"
@ -54,35 +63,6 @@ typedef struct tag_config {
int foreground; /**< Whether or not we are running in foreground */
int full_reload; /**< Whether the reload should be a full one */
#if 0
char *configfile; /**< path to config file */
char *web_root; /**< path to the dir containing the web files */
char *iface; /**< interface to advertise on */
int port; /**< port to listen on */
int rescan_interval; /**< How often to do a background fs rescan */
int always_scan; /**< 0 to minimize disk usage (embedded devices) */
int process_m3u; /**< Should we process m3u files? */
int scan_type; /**< Method for finding playtime. see scan-mp3.c */
int compress; /**< Should we compress? */
int latin1_tags; /**< interpret all tags as latin1 rather than utf8 */
char *adminpassword; /**< Password to web management pages */
char *readpassword; /**< iTunes password */
char *mp3dir; /**< root directory of the mp3 files */
char *servername; /**< Name advertised via rendezvous */
char *playlist; /**< Path to the playlist file */
char *runas; /**< Who to drop privs to (if run as root) */
char *dbdir; /**< Where to put the db file */
char *extensions; /**< What music file extentions to process */
char *ssc_codectypes; /**< What codectypes are converted in server */
char *ssc_prog; /**< Server side music format converter prog */
char *artfilename; /**< What filename to merge coverart with */
char *logfile; /**< What file to use as a logfile */
char *compdirs; /**< Compilations directories */
char *dbtype; /**< db backend type */
char *dbparms; /**< parameters for the db backend */
char **complist; /**< list of compilation directories */
#endif
STATS stats; /**< Stats structure (see above) */
WSHANDLE server; /**< webserver handle */
} CONFIG;

View File

@ -34,6 +34,7 @@
#include <stdlib.h>
#include <string.h>
#include "daapd.h"
#include "err.h"
#include "mp3-scanner.h"
#include "redblack.h"

View File

@ -39,6 +39,7 @@
# include <stdint.h>
#endif
#include "daapd.h"
#include "db-generic.h"
#include "err.h"
#include "mp3-scanner.h"

View File

@ -48,6 +48,7 @@
#include <stdint.h>
#endif
#include "daapd.h"
#include "conf.h"
#include "err.h"
#include "db-generic.h"

View File

@ -48,6 +48,7 @@
# include <unistd.h>
#endif
#include "daapd.h"
#include "conf.h"
#include "err.h"
#include "db-generic.h"

View File

@ -5,6 +5,7 @@
*/
#include <stdio.h>
#include "daapd.h"
char *db_sqlite_updates[] = {
/* version 0 -> version 1 -- initial update */

View File

@ -38,6 +38,7 @@
# include <stdint.h>
#endif
#include "daapd.h"
#include "err.h"
#include "mp3-scanner.h"
#include "db-generic.h"
@ -1624,6 +1625,7 @@ void db_sql_dispose_item(MP3FILE *pmp3) {
MAYBEFREE(pmp3->description);
MAYBEFREE(pmp3->url);
MAYBEFREE(pmp3->codectype);
MAYBEFREE(pmp3->album_artist);
free(pmp3);
}

View File

@ -40,6 +40,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include "daapd.h"
#include "conf.h"
#include "configfile.h"
#include "err.h"

View File

@ -47,6 +47,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include "daapd.h"
#include "err.h"
#ifndef ERR_LEAN
# include "os.h"

View File

@ -32,6 +32,7 @@
#endif
#include <stdio.h>
#include "daapd.h"
/* Comment out all this code if we are using the GNU C Library, and are not
actually compiling the library itself. This code is part of the GNU C

View File

@ -28,6 +28,7 @@
#include <stdlib.h>
#include <string.h>
#include "daapd.h"
#include "ll.h"
#include "err.h"

View File

@ -1016,7 +1016,7 @@ Merge in license terms from Quinn's copy, in preparation for Darwin release
#include "mDNSClientAPI.h" // Defines the interface provided to the client layer above
#include "mDNSPlatformFunctions.h" // Defines the interface required of the supporting layer below
#include "daapd.h"
// Disable certain benign warnings with Microsoft compilers
#if(defined(_MSC_VER))
// Disable "conditional expression is constant" warning for debug macros.

View File

@ -146,6 +146,7 @@ First checkin
#include <sys/uio.h>
#include <netinet/in.h>
#include "daapd.h"
#include "err.h"
#include "mDNSUNP.h"

View File

@ -91,6 +91,7 @@ First checkin
#include <unistd.h>
#include <stdio.h>
#include "daapd.h"
#include "err.h"
/* Solaris defined SIOCGIFCONF etc in <sys/sockio.h> but

View File

@ -76,6 +76,8 @@
#include <sys/wait.h>
#endif
#include "daapd.h"
#include "conf.h"
#include "configfile.h"
#include "err.h"
@ -578,6 +580,7 @@ int main(int argc, char *argv[]) {
DPRINTF(E_LOG,L_MAIN|L_DB|L_SCAN,"Scanned %d songs (was %d) in "
"%d seconds\n",song_count,old_song_count,
time(NULL)-start_time);
conf_dispose_array(mp3_dir_array);
}
os_wait(MAIN_SLEEP_INTERVAL);
@ -610,6 +613,7 @@ int main(int argc, char *argv[]) {
DPRINTF(E_LOG,L_MAIN,"Done!\n");
os_deinit();
mem_dump();
return EXIT_SUCCESS;
}

203
src/memdebug.c Normal file
View File

@ -0,0 +1,203 @@
/**
* memory debugging
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
#include "err.h"
#include "util.h"
#ifdef DEBUG_MEM
typedef struct debugnode_t {
char *file;
int line;
int size;
void *ptr;
struct debugnode_t *next;
} DEBUGNODE;
DEBUGNODE _debug_memlist = { NULL, 0, 0, NULL, NULL };
void debug_free(char *file, int line, void *ptr);
void *debug_malloc(char *file, int line, size_t size);
void *debug_realloc(char *file, int line, void *ptr, size_t size);
void *debug_calloc(char *file, int line, size_t count, size_t size);
char *debug_strdup(char *file, int line, const char *str);
void debug_dump(void);
/**
* find a ptr in the node list, assuming the list is already
* locked.
*/
DEBUGNODE *_debug_find_ptr(void *ptr) {
DEBUGNODE *prev, *current;
prev = &_debug_memlist;
current = prev->next;
while(current) {
if(current->ptr == ptr)
return current;
prev = current;
current = current->next;
}
return NULL;
}
void *_debug_alloc_nolock(char *file, int line, int size) {
DEBUGNODE *pnew;
pnew = (DEBUGNODE *)malloc(sizeof(DEBUGNODE));
if(!pnew)
DPRINTF(E_FATAL,L_MISC,"Malloc failed in _debug_alloc\n");
pnew->file = strdup(file);
pnew->line = line;
pnew->size = size;
pnew->ptr = malloc(size);
if(!pnew->ptr)
DPRINTF(E_FATAL,L_MISC,"Malloc failed in _debug_alloc\n");
pnew->next = _debug_memlist.next;
_debug_memlist.next = pnew;
return pnew->ptr;
}
void *_debug_alloc(char *file, int line, int size) {
void *ptr;
util_mutex_lock(l_memdebug);
ptr = _debug_alloc_nolock(file, line, size);
util_mutex_unlock(l_memdebug);
return ptr;
}
void debug_dump(void) {
DEBUGNODE *current;
uint32_t size = 0;
int blocks = 0;
if(!_debug_memlist.next) {
DPRINTF(E_LOG,L_MISC,"No leaked memory!\n");
return;
}
current = _debug_memlist.next;
while(current) {
size += current->size;
blocks++;
current = current->next;
}
DPRINTF(E_WARN,L_MISC,"Leaked %d bytes in %d blocks\n",size, blocks);
current = _debug_memlist.next;
while(current) {
DPRINTF(E_WARN,L_MISC,"%d bytes: %s, line %d\n", current->size,
current->file, current->line);
util_hexdump(current->ptr,current->size);
current = current->next;
}
}
void debug_free(char *file, int line, void *ptr) {
DEBUGNODE *prev, *current;
util_mutex_lock(l_memdebug);
prev = &_debug_memlist;
current = prev->next;
while(current) {
if(current->ptr == ptr) {
/* found it */
break;
}
prev = current;
current = current->next;
}
if(!current) {
util_mutex_unlock(l_memdebug);
DPRINTF(E_FATAL,L_MISC,"Attempt to free an unallocated ptr: %s, %d\n",
file, line);
}
prev->next = current->next;
util_mutex_unlock(l_memdebug);
if(current) {
if(current->file)
free(current->file);
if(current->ptr)
free(current->ptr);
free(current);
}
return;
}
void *debug_malloc(char *file, int line, size_t size) {
void *ptr;
ptr = _debug_alloc(file,line,size);
return ptr;
}
void *debug_realloc(char *file, int line, void *ptr, size_t size) {
void *new_ptr;
DEBUGNODE *old_node;
int copy_size;
util_mutex_lock(l_memdebug);
old_node = _debug_find_ptr(ptr);
if(!old_node) {
util_mutex_unlock(l_memdebug);
DPRINTF(E_FATAL,L_MISC,"Attempt to realloc invalid ptr: %s, %d\n",
file, line);
}
new_ptr = _debug_alloc_nolock(file, line, size);
util_mutex_unlock(l_memdebug);
copy_size = old_node->size;
if(size < copy_size)
copy_size=size;
memcpy(new_ptr,old_node->ptr,copy_size);
debug_free(file, line, old_node->ptr);
return new_ptr;
}
void *debug_calloc(char *file, int line, size_t count, size_t size) {
void *ptr;
ptr = debug_malloc(file, line, count * size);
memset(ptr,0,count *size);
return ptr;
}
char *debug_strdup(char *file, int line, const char *str) {
void *ptr;
int size;
size = strlen(str);
ptr = debug_malloc(file, line, size + 1);
strcpy(ptr,str);
return ptr;
}
#endif

23
src/memdebug.h Normal file
View File

@ -0,0 +1,23 @@
/*
* $Id: $
*/
#ifdef DEBUG_MEM
# define free(ptr) debug_free(__FILE__,__LINE__,(ptr))
# define malloc(size) debug_malloc(__FILE__,__LINE__,(size))
# define realloc(ptr, size) debug_realloc(__FILE__,__LINE__,(ptr),(size))
# define calloc(count,size) debug_calloc(__FILE__,__LINE__,(count),(size))
# define strdup(str) debug_strdup(__FILE__,__LINE__,(str))
# define mem_dump() debug_dump()
extern void debug_free(char *file, int line, void *ptr);
extern void *debug_malloc(char *file, int line, size_t size);
extern void *debug_realloc(char *file, int line, void *ptr, size_t size);
extern void *debug_calloc(char *file, int line, size_t count, size_t size);
extern void *debug_strdup(char *file, int line, const char *str);
extern void debug_dump(void);
#else
# define mem_dump()
#endif

View File

@ -46,8 +46,8 @@
#endif
#include <sys/stat.h>
#include "conf.h"
#include "daapd.h"
#include "conf.h"
#include "db-generic.h"
#include "err.h"
#include "mp3-scanner.h"

View File

@ -52,9 +52,9 @@
#include "CoreFoundation/CoreFoundation.h"
#endif
#include "daapd.h"
#include "conf.h"
#include "err.h"
#include "daapd.h"
#include "os.h"
/** You say po-tay-to, I say po-tat-o */

View File

@ -8,11 +8,11 @@
#include <string.h>
#include <unistd.h>
#include "daapd.h"
#include "conf.h"
#include "db-generic.h"
#include "err.h"
#include "smart-parser.h"
#include "daapd.h"
CONFIG config;
char *scan_winamp_genre[] = { NULL };

View File

@ -45,6 +45,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include "daapd.h"
#include "conf.h"
#include "configfile.h"
#include "db-generic.h"

View File

@ -33,6 +33,8 @@ static char rcsid[]="$Id$";
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include "daapd.h"
#include "redblack.h"
#define assert(expr)

View File

@ -41,6 +41,7 @@
#include <avahi-common/timeval.h>
#include <avahi-common/malloc.h>
#include "daapd.h"
#include "err.h"
static AvahiClient *mdns_client = NULL;

View File

@ -32,6 +32,7 @@
#include <howl.h>
#include <pthread.h>
#include "daapd.h"
#include "err.h"
#include "os-unix.h"
#include "rend-unix.h"

View File

@ -19,6 +19,7 @@
#include <sys/types.h>
#include "daapd.h"
#include "dns_sd.h"
#include "err.h"

View File

@ -52,6 +52,7 @@
# include <sys/wait.h>
#endif
#include "daapd.h"
#include "err.h"
#include "restart.h"

View File

@ -11,6 +11,7 @@
#include <stdlib.h>
#include <string.h>
#include "daapd.h"
#include "rxml.h"
/* Typedefs/Defines */

View File

@ -34,6 +34,7 @@
#include <netinet/in.h>
#endif
#include "daapd.h"
#include "err.h"
#include "mp3-scanner.h"
#include "scan-aac.h"

View File

@ -33,6 +33,7 @@
#include <sys/types.h>
#include "daapd.h"
#include "err.h"
#include "mp3-scanner.h"

View File

@ -44,8 +44,8 @@
#include <sys/stat.h>
#include "restart.h"
#include "daapd.h"
#include "restart.h"
#include "err.h"
#include "mp3-scanner.h"

View File

@ -34,8 +34,8 @@
#include <string.h>
#include <time.h>
#include "conf.h"
#include "daapd.h"
#include "conf.h"
#include "err.h"
#include "mp3-scanner.h"

View File

@ -34,6 +34,7 @@
#include <netinet/in.h>
#endif
#include "daapd.h"
#include "err.h"
#include "mp3-scanner.h"
#include "scan-aac.h"

View File

@ -31,6 +31,7 @@
#include <string.h>
#include <taglib/tag_c.h>
#include "daapd.h"
#include "mp3-scanner.h"
#include "err.h"

View File

@ -20,6 +20,8 @@
#include <string.h>
#include <sys/stat.h>
#include <vorbis/vorbisfile.h>
#include "daapd.h"
#include "err.h"
#include "mp3-scanner.h"

View File

@ -30,6 +30,7 @@
#include <stdlib.h>
#include <string.h>
#include "daapd.h"
#include "err.h"
#include "mp3-scanner.h"

View File

@ -30,6 +30,7 @@
#include <string.h>
#include <fcntl.h>
#include "daapd.h"
#include "err.h"
#include "mp3-scanner.h"

View File

@ -32,6 +32,7 @@
#include <stdlib.h>
#include <string.h>
#include "daapd.h"
#include "mp3-scanner.h"
#include "restart.h"
#include "err.h"

View File

@ -467,6 +467,12 @@ int scan_xml_playlist(char *filename) {
}
rbdestroy(scan_xml_db);
MAYBEFREE(scan_xml_itunes_version);
MAYBEFREE(scan_xml_itunes_base_path);
MAYBEFREE(scan_xml_itunes_decoded_base_path);
MAYBEFREE(scan_xml_real_base_path);
return retval;
}
@ -956,6 +962,7 @@ int scan_xml_playlists_section(int action, char *info) {
}
dont_scan=0;
state=XML_PL_ST_EXPECTING_PL_TRACKLIST;
MAYBEFREE(current_name);
return XML_STATE_PLAYLISTS;
}
if(action == RXML_EVT_BEGIN)
@ -967,8 +974,7 @@ int scan_xml_playlists_section(int action, char *info) {
if(action == RXML_EVT_TEXT) {
/* got the value we were hoping for */
if(next_value == XML_PL_NEXT_VALUE_NAME) {
if(current_name)
free(current_name);
MAYBEFREE(current_name);
current_name = strdup(info);
DPRINTF(E_DBG,L_SCAN,"Found playlist: %s\n",current_name);
/* disallow specific playlists */

View File

@ -29,8 +29,8 @@
#include <string.h>
#include <unistd.h>
#include "conf.h"
#include "daapd.h"
#include "conf.h"
#include "err.h"
#include "mp3-scanner.h"

View File

@ -4,8 +4,8 @@
#include <stdlib.h>
#include <string.h>
#include "conf.h"
#include "daapd.h"
#include "conf.h"
#include "err.h"
#include "os.h"
#include "plugin.h"

View File

@ -42,6 +42,8 @@
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "daapd.h"
#include "uici.h"
#ifndef MAXBACKLOG

View File

@ -31,7 +31,6 @@ int _util_initialized=0;
/* Forwards */
//int _util_xtoy(unsigned char *dbuffer, size_t dlen, unsigned char *sbuffer, size_t slen, char *from, char *to);
void _util_hexdump(unsigned char *block, int len);
void _util_mutex_init(void);
/**
@ -399,7 +398,7 @@ int _util_xtoy(unsigned char *dbuffer, size_t dlen, unsigned char *sbuffer, size
}
*/
void _util_hexdump(unsigned char *block, int len) {
void util_hexdump(unsigned char *block, int len) {
char charmap[256];
int index;
int row, offset;
@ -429,7 +428,7 @@ void _util_hexdump(unsigned char *block, int len) {
strcat(output,tmp);
}
fprintf(stderr,"%s\n",output);
DPRINTF(E_LOG,L_MISC,"%s\n",output);
}
}

View File

@ -19,6 +19,7 @@ typedef enum {
l_err,
l_conf,
l_plugin,
l_memdebug,
l_last
} lock_t;
@ -46,6 +47,7 @@ extern int util_utf8toutf16(unsigned char *utf16, int dlen, unsigned char *utf8,
extern int util_utf16toutf8(unsigned char *utf8, int dlen, unsigned char *utf16, int len);
extern int util_utf16_byte_len(unsigned char *utf16);
extern void util_hexdump(unsigned char *block, int len);
#endif /* _UTIL_H_ */

View File

@ -27,6 +27,7 @@
#include <windows.h>
#include <stdio.h>
#include "daapd.h"
#include "messages.h"
static HANDLE elog_handle = NULL;

View File

@ -41,6 +41,7 @@
# include <sys/socket.h>
#endif
#include "daapd.h"
#include "err.h"
#include "restart.h"
#include "webserver.h"

View File

@ -29,10 +29,11 @@
#include <sys/stat.h>
#include <sys/types.h>
#include "daapd.h"
#include "configfile.h"
#include "conf.h"
#include "db-generic.h"
#include "daapd.h"
#include "err.h"
#include "mp3-scanner.h"
#include "os.h"