summaryrefslogtreecommitdiff
path: root/libmailutils
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2021-11-03 18:05:55 +0200
committerSergey Poznyakoff <gray@gnu.org>2021-11-03 18:05:55 +0200
commit91d35ab2ee423a0511aa7b48eea173ca3706f4bf (patch)
tree9b5ff17787483afc5b7e44d7a23ea7e8cd5beb86 /libmailutils
parent22ceaddfb850e3b9692fe2a75a83ec0156345ac8 (diff)
downloadmailutils-91d35ab2ee423a0511aa7b48eea173ca3706f4bf.tar.gz
mailutils-91d35ab2ee423a0511aa7b48eea173ca3706f4bf.tar.bz2
Free some allocated memory on exit.
This eliminates certain "memory leaks", that have little (if any) practical importance, bu make leak detection tools overly vociferous. * libmailutils/base/monitor.c (static_monitor_dealloc): New function. (mu_monitor_rdlock, mu_monitor_wrlock): Install static_monitor_dealloc as onexit handler. * libmailutils/filter/filter.c (filter_dealloc): New function: deallocate filter_list. (mu_filter_get_list): Install filter_dealloc as onexit handler. * libmailutils/locus/ident.c (nametab_dealloc): New function. (mu_ident_ref): Install nametab_dealloc as onexit handler. * libmailutils/stdstream/basestr.c (std_log_bootstrap): Unref transport after setting it up to log stream. (stdstream_flushall): Call mu_stream_destroy instead of mu_stream_flush.
Diffstat (limited to 'libmailutils')
-rw-r--r--libmailutils/base/monitor.c19
-rw-r--r--libmailutils/filter/filter.c13
-rw-r--r--libmailutils/locus/ident.c8
-rw-r--r--libmailutils/stdstream/basestr.c9
4 files changed, 41 insertions, 8 deletions
diff --git a/libmailutils/base/monitor.c b/libmailutils/base/monitor.c
index 2ffb93552..58dbab1f3 100644
--- a/libmailutils/base/monitor.c
+++ b/libmailutils/base/monitor.c
@@ -34,6 +34,7 @@
#include <mailutils/sys/monitor.h>
#include <mailutils/errno.h>
+#include <mailutils/util.h>
#ifdef WITH_PTHREAD
pthread_mutex_t monitor_lock = PTHREAD_MUTEX_INITIALIZER;
@@ -72,8 +73,9 @@ static int monitor_pthread_unlock (p_lock_t);
the concrete implementation of monitor on pthread and read/write locks,
but changing to a different concrete implementation will not be hard if
the need arise.
- For static initializers we take a small penality and since we have
- a global static lock.
+ Static initializers are allocated at the first call to
+ mu_monitor_(rd|wr)lock and are freed on program exit (or a call to
+ mu_onexit_run, whichever happens first).
*/
int
@@ -126,6 +128,17 @@ mu_monitor_destroy (mu_monitor_t *pmonitor, void *owner)
}
}
+static void
+static_monitor_dealloc (void *ptr)
+{
+ mu_monitor_t monitor = ptr;
+ STATIC_LOCK (&monitor_lock);
+ if (monitor->flags == MU_MONITOR_PTHREAD)
+ monitor_pthread_destroy ((p_lock_t *)&(monitor->data));
+ monitor->allocated = 0;
+ STATIC_UNLOCK (&monitor_lock);
+}
+
int
mu_monitor_rdlock (mu_monitor_t monitor)
{
@@ -143,6 +156,7 @@ mu_monitor_rdlock (mu_monitor_t monitor)
STATIC_UNLOCK (&monitor_lock);
return status;
}
+ mu_onexit (static_monitor_dealloc, monitor);
}
monitor->allocated = 1;
STATIC_UNLOCK (&monitor_lock);
@@ -170,6 +184,7 @@ mu_monitor_wrlock (mu_monitor_t monitor)
STATIC_UNLOCK (&monitor_lock);
return status;
}
+ mu_onexit (static_monitor_dealloc, monitor);
}
monitor->allocated = 1;
STATIC_UNLOCK (&monitor_lock);
diff --git a/libmailutils/filter/filter.c b/libmailutils/filter/filter.c
index 00f8c45e1..45c8d49be 100644
--- a/libmailutils/filter/filter.c
+++ b/libmailutils/filter/filter.c
@@ -38,9 +38,8 @@
#include <mailutils/stream.h>
#include <mailutils/errno.h>
#include <mailutils/cstr.h>
+#include <mailutils/util.h>
-/* NOTE: We will leak here since the monitor of the filter will never
- be release. That's ok we can leave with this, it's only done once. */
static mu_list_t filter_list;
struct mu_monitor filter_monitor = MU_MONITOR_INITIALIZER;
@@ -52,6 +51,14 @@ filter_name_cmp (const void *item, const void *data)
return mu_c_strcasecmp (rec->name, name);
}
+static void
+filter_dealloc (void *x)
+{
+ mu_monitor_wrlock (&filter_monitor);
+ mu_list_destroy (&filter_list);
+ mu_monitor_unlock (&filter_monitor);
+}
+
int
mu_filter_get_list (mu_list_t *plist)
{
@@ -91,6 +98,8 @@ mu_filter_get_list (mu_list_t *plist)
mu_list_append (filter_list, mu_fromrb_filter);
/* FIXME: add the default encodings? */
+
+ mu_onexit (filter_dealloc, NULL);
}
*plist = filter_list;
mu_monitor_unlock (&filter_monitor);
diff --git a/libmailutils/locus/ident.c b/libmailutils/locus/ident.c
index 36cb490af..28cdfe49a 100644
--- a/libmailutils/locus/ident.c
+++ b/libmailutils/locus/ident.c
@@ -26,6 +26,7 @@
#include <mailutils/io.h>
#include <mailutils/stream.h>
#include <mailutils/iterator.h>
+#include <mailutils/util.h>
struct mu_ident_ref
{
@@ -34,6 +35,12 @@ struct mu_ident_ref
static mu_assoc_t nametab;
+static void
+nametab_dealloc (void *ptr)
+{
+ mu_assoc_destroy (&nametab);
+}
+
int
mu_ident_ref (char const *name, char const **refname)
{
@@ -57,6 +64,7 @@ mu_ident_ref (char const *name, char const **refname)
return rc;
}
mu_assoc_set_destroy_item (nametab, mu_list_free_item);
+ mu_onexit (nametab_dealloc, NULL);
}
rc = mu_assoc_install_ref2 (nametab, name, &refptr, refname);
switch (rc)
diff --git a/libmailutils/stdstream/basestr.c b/libmailutils/stdstream/basestr.c
index 81ed512cf..922cf469f 100644
--- a/libmailutils/stdstream/basestr.c
+++ b/libmailutils/stdstream/basestr.c
@@ -104,7 +104,8 @@ std_log_bootstrap (struct _mu_stream *str, int code,
str->event_cb_data = 0;
_mu_log_stream_setup (logstr, transport);
- stdstream_flushall_setup ();
+ mu_stream_unref (transport);
+ stdstream_flushall_setup ();
}
/* The noop destroy function is necessary to prevent stream core from
@@ -161,9 +162,9 @@ mu_stream_t mu_strerr = (mu_stream_t) &default_strerr;
static void
stdstream_flushall (void *data MU_ARG_UNUSED)
{
- mu_stream_flush (mu_strin);
- mu_stream_flush (mu_strout);
- mu_stream_flush (mu_strerr);
+ mu_stream_destroy (&mu_strin);
+ mu_stream_destroy (&mu_strout);
+ mu_stream_destroy (&mu_strerr);
}
static void

Return to:

Send suggestions and report system problems to the System administrator.