diff options
author | Christian C <cc@localhost> | 2025-04-02 18:15:13 -0700 |
---|---|---|
committer | Christian C <cc@localhost> | 2025-04-02 18:15:13 -0700 |
commit | 00ce37145556279f4982ef52a747cb2f5e3e3081 (patch) | |
tree | aeee1e555d11bed381f1fdb34f3d95ba1aa05ea8 /lib | |
parent | ec7436a01deb8e28743de47ad98950c914d6da2a (diff) |
Fixed Memory Leak
Diffstat (limited to 'lib')
-rw-r--r-- | lib/data/image_types.c | 44 | ||||
-rw-r--r-- | lib/dir.c | 21 | ||||
-rw-r--r-- | lib/mem/galloc.c | 29 |
3 files changed, 72 insertions, 22 deletions
diff --git a/lib/data/image_types.c b/lib/data/image_types.c index f5c9115..6e8f6b4 100644 --- a/lib/data/image_types.c +++ b/lib/data/image_types.c @@ -68,29 +68,45 @@ Mask *create_image_mask(size_t width, size_t height) { } void free_image(Image *image) { - if (image->image[0] != NULL) { - g_free(image->image[0]); - image->image[0] = NULL; + if (image == NULL) { + return; } - if (image->image != NULL) { - g_free(image->image); - image->image = NULL; + if (image->image == NULL) { + g_free(image); + return; } - if (image != NULL) { + if (image->image[0] != NULL) { + if (image->image[0][0] != NULL) { + g_free(image->image[0][0]); + } + } else { + g_free(image->image); g_free(image); + return; + } + for (size_t y = 0; y < image->height; y++) { + if (image->image[y] != NULL) { + g_free(image->image[y]); + } } + g_free(image->image); + image->image = NULL; + g_free(image); } void free_image_mask(Mask *image_mask) { + if (image_mask == NULL) { + return; + } + if (image_mask->image == NULL) { + g_free(image_mask); + return; + } if (image_mask->image[0] != NULL) { g_free(image_mask->image[0]); image_mask->image[0] = NULL; } - if (image_mask->image != NULL) { - g_free(image_mask->image); - image_mask->image = NULL; - } - if (image_mask != NULL) { - g_free(image_mask); - } + g_free(image_mask->image); + image_mask->image = NULL; + g_free(image_mask); } @@ -22,7 +22,7 @@ char **list_directory(char *dirname) { struct dirent *dir; d = opendir(dirname); char **file_names = (char **)g_malloc(sizeof(char *)); - if (!file_names) { + if (file_names == NULL) { return NULL; } file_names[0] = NULL; @@ -32,10 +32,24 @@ char **list_directory(char *dirname) { if (dir->d_type == DT_REG) { // When a regular file is reached /// Create space for it in the list - file_names = realloc(file_names, (file_count + 1 + 1) * sizeof(char *)); + char **temp = + g_realloc(file_names, (file_count + 1 + 1) * sizeof(char *)); + if (temp == NULL) { + for (size_t file_idx = 0; file_idx < file_count; file_idx++) { + g_free(file_names[file_idx]); + } + return NULL; + } + file_names = temp; /// Create space for the name file_names[file_count] = g_calloc(strlen(dir->d_name) + 1, sizeof(char)); + if (file_names[file_count] == NULL) { + for (size_t file_idx = 0; file_idx < file_count; file_idx++) { + g_free(file_names[file_idx]); + } + return NULL; + } /// Copy the name strcpy(file_names[file_count], dir->d_name); /// Mark the end of the file list with a NULL entry @@ -53,6 +67,9 @@ char *full_path(char *dir, char *file) { size_t dir_len = strlen(dir); size_t file_len = strlen(file); fpath = (char *)g_calloc(dir_len + file_len + 2, sizeof(char)); + if (fpath == NULL) { + return NULL; + } strcpy(fpath, dir); strcpy(fpath + dir_len + 1, file); fpath[dir_len] = '/'; diff --git a/lib/mem/galloc.c b/lib/mem/galloc.c index 642e010..ea4ea16 100644 --- a/lib/mem/galloc.c +++ b/lib/mem/galloc.c @@ -1,29 +1,46 @@ -#include <lib/mem/galloc.h> + #include <lib/mem/galloc.h> #include <stdlib.h> +#include <stdio.h> static ssize_t standing_allocations = 0; -void *g_malloc(size_t size) { +void *_g_malloc(size_t size, char* file, unsigned int line) { void *ptr = malloc(size); if (ptr == NULL) { return ptr; } + fprintf(stderr, "+%p :a: %s:%d\n", ptr, file, line); standing_allocations++; return ptr; } -void *g_calloc(size_t n_memb, size_t size) { +void *_g_calloc(size_t n_memb, size_t size, char* file, unsigned int line) { void *ptr = calloc(n_memb, size); if (ptr == NULL) { return ptr; } + fprintf(stderr, "+%p :c: %s:%d\n", ptr, file, line); standing_allocations++; return ptr; } -void g_free(void *ptr) { - free(ptr); - standing_allocations--; +void *_g_realloc(void *ptr, size_t size, char* file, unsigned int line) { + fprintf(stderr, "-%p :r: %s:%d\n", ptr, file, line); + void* temp = realloc(ptr, size); + if (temp == NULL) { + fprintf(stderr, "+%p :r: %s:%d\n", ptr, file, line); + } else { + fprintf(stderr, "+%p :r: %s:%d\n", temp, file, line); + } + return temp; +} + +void _g_free(void *ptr, char* file, unsigned int line) { + if (ptr != NULL) { + fprintf(stderr, "-%p :f: %s:%d\n", ptr, file, line); + free(ptr); + standing_allocations--; + } } ssize_t g_outstanding_allocations() { return standing_allocations; } |