commit 1edfdc77b42d647384c7ef09e2a2b0a59e69633a
parent d9ae2aff1dbefb80cdb64991ce60b7193ab16173
Author: TheJackiMonster <thejackimonster@gmail.com>
Date: Mon, 22 Nov 2021 16:04:03 +0100
Added shutdown hook to cleanup memory leaks
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
Diffstat:
3 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c
@@ -38,6 +38,9 @@ handle_create_from_config (const struct GNUNET_CONFIGURATION_Handle* cfg,
struct GNUNET_CHAT_Handle* handle = GNUNET_new(struct GNUNET_CHAT_Handle);
handle->cfg = cfg;
+ handle->shutdown_hook = GNUNET_SCHEDULER_add_shutdown(
+ on_handle_shutdown, handle
+ );
if ((directory) &&
(GNUNET_YES == GNUNET_DISK_directory_test(directory, GNUNET_YES)))
@@ -120,6 +123,9 @@ handle_destroy (struct GNUNET_CHAT_Handle* handle)
(handle->contexts) &&
(handle->files));
+ if (handle->shutdown_hook)
+ GNUNET_SCHEDULER_cancel(handle->shutdown_hook);
+
if (handle->public_key)
GNUNET_free(handle->public_key);
diff --git a/src/gnunet_chat_handle.h b/src/gnunet_chat_handle.h
@@ -32,6 +32,7 @@
#include <gnunet/gnunet_arm_service.h>
#include <gnunet/gnunet_fs_service.h>
#include <gnunet/gnunet_messenger_service.h>
+#include <gnunet/gnunet_scheduler_lib.h>
#include <gnunet/gnunet_util_lib.h>
#include "gnunet_chat_lib.h"
@@ -39,6 +40,7 @@
struct GNUNET_CHAT_Handle
{
const struct GNUNET_CONFIGURATION_Handle* cfg;
+ struct GNUNET_SCHEDULER_Task *shutdown_hook;
char *directory;
diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c
@@ -34,7 +34,19 @@
#define GNUNET_UNUSED __attribute__ ((unused))
void
-on_handle_arm_connection(void *cls, int connected)
+on_handle_shutdown(void *cls)
+{
+ struct GNUNET_CHAT_Handle *chat = cls;
+
+ GNUNET_assert((chat) && (chat->shutdown_hook));
+ chat->shutdown_hook = NULL;
+
+ handle_destroy(chat);
+}
+
+void
+on_handle_arm_connection(void *cls,
+ int connected)
{
struct GNUNET_CHAT_Handle *chat = cls;