summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-01-22 19:17:19 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-01-22 19:17:19 -0500
commitf2edda528a3babe16d9da48580bdcb696e5fa664 (patch)
tree8e54df107de0bc3b87d147bb973e51eef76b769b
parent0f0a03b518ff6dbb61814896e5efc9b5878904e4 (diff)
fixed memory leaks
-rw-r--r--arr.c31
-rw-r--r--config.c2
-rw-r--r--main.c4
-rw-r--r--menu.c2
-rw-r--r--ui.c6
-rw-r--r--valgrind_suppresions.txt99
6 files changed, 129 insertions, 15 deletions
diff --git a/arr.c b/arr.c
index 75c5d90..0491ef4 100644
--- a/arr.c
+++ b/arr.c
@@ -109,7 +109,7 @@ static inline void *arr_add_ptr_(void **arr, size_t member_size) {
return ret;
}
-static inline void arr_set_len_(void **arr, size_t member_size, size_t n) {
+static void arr_reserve_(void **arr, size_t member_size, size_t n) {
if (n >= U32_MAX-1) {
// too big; free arr.
if (*arr) free(arr_hdr_(*arr));
@@ -121,7 +121,6 @@ static inline void arr_set_len_(void **arr, size_t member_size, size_t n) {
ArrHeader *hdr = calloc(1, sizeof(ArrHeader) + (n+1) * member_size);
if (hdr) {
hdr->cap = (u32)n+1;
- hdr->len = (u32)n;
*arr = hdr->data;
}
} else {
@@ -146,15 +145,22 @@ static inline void arr_set_len_(void **arr, size_t member_size, size_t n) {
return;
}
}
+ *arr = hdr->data;
+ }
+}
+
+static void arr_set_len_(void **arr, size_t member_size, size_t n) {
+ arr_reserve_(arr, member_size, n);
+ if (*arr) {
+ ArrHeader *hdr = arr_hdr_(*arr);
if (n > hdr->len) {
// zero new elements
memset((char *)hdr->data + hdr->len, 0, (n - hdr->len) * member_size);
}
hdr->len = (u32)n;
- *arr = hdr->data;
}
}
-#define arr_set_len(a, n) arr_set_len_((void **)&a, sizeof *(a), n)
+
#ifdef __cplusplus
#define arr_cast_typeof(a) (decltype(a))
@@ -207,19 +213,24 @@ static inline void arr_set_len_(void **arr, size_t member_size, size_t n) {
} \
} while (0)
+// Ensure that enough space is allocated for n elements.
+#define arr_reserve(a, n) arr_reserve_((void **)&(a), sizeof *(a), (n))
+// Similar to arr_reserve, but also sets the length of the array to n.
+#define arr_set_len(a, n) arr_set_len_((void **)&(a), sizeof *(a), (n))
-static void arr_append_strn_(char **a, char const *s, size_t s_len) {
+static void arrcstr_append_strn_(char **a, char const *s, size_t s_len) {
size_t curr_len = arr_len(*a);
- if (curr_len) --curr_len; // don't include null terminator
-
- arr_set_len(*a, curr_len + s_len + 1);
+ size_t new_len = curr_len + s_len;
+ arr_reserve(*a, new_len + 1);
+ arr_set_len(*a, new_len);
memcpy(*a + curr_len, s, s_len + 1);
}
// append to a C-string array
-#define arr_append_str(a, s) arr_append_strn_(&(a), (s), strlen(s))
+#define arrcstr_append_str(a, s) arrcstr_append_strn_(&(a), (s), strlen(s))
// take at most n bytes from s
-#define arr_append_strn(a, s, n) arr_append_strn_(&(a), (s), (n))
+#define arrcstr_append_strn(a, s, n) arrcstr_append_strn_(&(a), (s), (n))
+
static void arr_test(void) {
u32 *arr = NULL;
diff --git a/config.c b/config.c
index 4afc203..9a2bed8 100644
--- a/config.c
+++ b/config.c
@@ -319,7 +319,7 @@ void config_read(Ted *ted, char const *filename) {
}
}
} else {
- config_err(cfg, "Invalid line syntax."
+ config_err(cfg, "Invalid line syntax. "
"Lines should either look like [section-name] or key = value");
}
} break;
diff --git a/main.c b/main.c
index 2c35958..f04f984 100644
--- a/main.c
+++ b/main.c
@@ -436,12 +436,16 @@ int main(int argc, char **argv) {
SDL_GL_SwapWindow(window);
}
+ if (ted->menu)
+ menu_close(ted, false); // free any memory used by the current menu
+
SDL_GL_DeleteContext(glctx);
SDL_DestroyWindow(window);
SDL_Quit();
buffer_free(&ted->main_buffer);
buffer_free(&ted->line_buffer);
text_font_free(ted->font);
+ text_font_free(ted->font_bold);
free(ted);
#if _WIN32
for (int i = 0; i < argc; ++i)
diff --git a/menu.c b/menu.c
index af47393..7e1db53 100644
--- a/menu.c
+++ b/menu.c
@@ -15,7 +15,7 @@ static void menu_close(Ted *ted, bool restore_prev_active_buffer) {
ted->menu = MENU_NONE;
if (restore_prev_active_buffer) ted->active_buffer = ted->prev_active_buffer;
ted->prev_active_buffer = NULL;
-
+ file_selector_free(&ted->file_selector);
buffer_clear(&ted->line_buffer);
}
diff --git a/ui.c b/ui.c
index 57b6177..5d31d29 100644
--- a/ui.c
+++ b/ui.c
@@ -61,8 +61,8 @@ void file_selector_cd(Ted *ted, FileSelector *fs, char const *path) {
--path_len;
// add path separator to end
- arr_append_str(fs->cwd, PATH_SEPARATOR_STR);
- arr_append_strn(fs->cwd, path, path_len);
+ arrcstr_append_str(fs->cwd, PATH_SEPARATOR_STR);
+ arrcstr_append_strn(fs->cwd, path, path_len);
// clear search term
buffer_clear(&ted->line_buffer);
@@ -74,7 +74,7 @@ static char *file_selector_update(Ted *ted, FileSelector *fs) {
String32 search_term32 = buffer_get_line(&ted->line_buffer, 0);
if (!fs->cwd) {
// set the file selector's directory to our current directory.
- arr_append_str(fs->cwd, ted->cwd);
+ arrcstr_append_str(fs->cwd, ted->cwd);
}
char *search_term = search_term32.len ? str32_to_utf8_cstr(search_term32) : NULL;
diff --git a/valgrind_suppresions.txt b/valgrind_suppresions.txt
index 45424fb..388ace8 100644
--- a/valgrind_suppresions.txt
+++ b/valgrind_suppresions.txt
@@ -7,6 +7,98 @@
}
{
<insert_a_suppression_name_here>
+ Memcheck:Leak
+ ...
+ fun:dbus_threads_init
+ ...
+}
+{
+ <insert_a_suppression_name_here>
+ Memcheck:Leak
+ ...
+ fun:dbus_bus_register
+ ...
+}
+{
+ <insert_a_suppression_name_here>
+ Memcheck:Leak
+ ...
+ fun:__glDispatchMakeCurrent
+ ...
+}
+{
+ <insert_a_suppression_name_here>
+ Memcheck:Leak
+ ...
+ fun:_dbus_type_writer_recurse
+ ...
+}
+{
+ <insert_a_suppression_name_here>
+ Memcheck:Leak
+ ...
+ fun:_XimOpenIM
+ ...
+}
+{
+ <insert_a_suppression_name_here>
+ Memcheck:Leak
+ ...
+ fun:_dbus_register_shutdown_func
+ ...
+}
+{
+ <insert_a_suppression_name_here>
+ Memcheck:Leak
+ ...
+ fun:XInitThreads
+ ...
+}
+{
+ <insert_a_suppression_name_here>
+ Memcheck:Leak
+ match-leak-kinds: reachable
+ fun:malloc
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ fun:main
+}
+{
+ <insert_a_suppression_name_here>
+ Memcheck:Leak
+ match-leak-kinds: reachable
+ fun:malloc
+ fun:realloc
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ fun:config_parse_key_combo
+ fun:config_read
+ fun:main
+}
+{
+ <insert_a_suppression_name_here>
+ Memcheck:Leak
+ match-leak-kinds: reachable
+ fun:malloc
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ obj:/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.14.0
+ fun:config_parse_key_combo
+ fun:config_read
+ fun:main
+}
+{
+ <insert_a_suppression_name_here>
Memcheck:Addr8
obj:/usr/lib/x86_64-linux-gnu/libdrm.so.2.4.0
obj:/usr/lib/x86_64-linux-gnu/libdrm.so.2.4.0
@@ -119,6 +211,13 @@
fun:main
}
{
+ name
+ Memcheck:Leak
+ ...
+ fun:XSetLocaleModifiers
+ ...
+}
+{
<insert_a_suppression_name_here>
Memcheck:Leak
match-leak-kinds: reachable