diff --git a/TODO.txt b/TODO.txt index 9c7b85e..167414d 100644 --- a/TODO.txt +++ b/TODO.txt @@ -59,7 +59,7 @@ Phase 2: Building a foundation [x] Allow logging to the syslog [ ] Fix bug where the socket stays open after quit. [ ] Figure out how to write unit tests for array/hashmap/etc -[ ] Fix memory leaks +[x] Fix memory leaks [ ] Make memory info access O(1) Phase 3: Welcome to Matrix diff --git a/src/HashMap.c b/src/HashMap.c index 8f2b9fd..e09a5f7 100644 --- a/src/HashMap.c +++ b/src/HashMap.c @@ -82,6 +82,7 @@ HashMapGrow(HashMap * map) newEntries = Malloc(map->capacity * sizeof(HashMapBucket *)); if (!newEntries) { + map->capacity /= 2; return 0; } diff --git a/src/Http.c b/src/Http.c index c1b02cc..ec74b9a 100644 --- a/src/Http.c +++ b/src/Http.c @@ -424,7 +424,7 @@ HttpParamDecode(char *in) { /* Malformed param */ Free(buf); - Free(params); + HashMapFree(params); return NULL; } diff --git a/src/Memory.c b/src/Memory.c index aec5d51..22ad6a5 100644 --- a/src/Memory.c +++ b/src/Memory.c @@ -90,11 +90,16 @@ MemoryAllocate(size_t size, const char *file, int line) } void * -MemoryReallocate(void *p, size_t size) +MemoryReallocate(void *p, size_t size, const char *file, int line) { MemoryInfo *a; void *new = NULL; + if (!p) + { + return MemoryAllocate(size, file, line); + } + pthread_mutex_lock(&lock); a = lastAllocation; @@ -107,6 +112,8 @@ MemoryReallocate(void *p, size_t size) { a->pointer = new; a->size = size; + a->file = file; + a->line = line; if (hook) { diff --git a/src/include/Memory.h b/src/include/Memory.h index 95ef7bd..8769cb5 100644 --- a/src/include/Memory.h +++ b/src/include/Memory.h @@ -35,13 +35,13 @@ typedef enum MemoryAction } MemoryAction; #define Malloc(x) MemoryAllocate(x, __FILE__, __LINE__) -#define Realloc(x, s) MemoryReallocate(x, s) +#define Realloc(x, s) MemoryReallocate(x, s, __FILE__, __LINE__) #define Free(x) MemoryFree(x) typedef struct MemoryInfo MemoryInfo; extern void *MemoryAllocate(size_t, const char *, int); -extern void *MemoryReallocate(void *, size_t); +extern void *MemoryReallocate(void *, size_t, const char *, int); extern void MemoryFree(void *); extern size_t MemoryAllocated(void);