Fix several memory leaks caused by configuration reloading
Hello all, Patch 1 is try to resolve below valgrind memory leak: ==25354== 26,112 bytes in 32 blocks are definitely lost in loss record 619 of 619 ==25354== at 0x4A05F58: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) ==25354== by 0x3A05A478F4: g_malloc (in /lib64/libglib-2.0.so.0.2600.0) ==25354== by 0x3A036416D2: log_writer_flush (logwriter.c:1020) ==25354== by 0x3A03641812: log_writer_deinit (logwriter.c:1138) ==25354== by 0x523AF4A: affile_dw_deinit (logpipe.h:268) ==25354== by 0x523B7EB: affile_dd_deinit (logpipe.h:268) ==25354== by 0x3A0362961E: cfg_tree_stop (logpipe.h:268) ==25354== by 0x3A036420DF: main_loop_reload_config_apply (mainloop.c:498) ==25354== by 0x3A036613FA: iv_signal_event (iv_signal.c:170) ==25354== by 0x3A0365FE48: iv_event_raw_got_event (iv_event_raw_posix.c:89) ==25354== by 0x3A03660511: iv_fd_poll_and_run (iv_fd.c:163) ==25354== by 0x3A03660C93: iv_main (iv_main_posix.c:117) Patch 3 is try to resolve below valgrind memory leak: ==25354== 1,107 (176 direct, 931 indirect) bytes in 1 blocks are definitely lost in loss record 594 of 619 ==25354== at 0x4A05F58: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) ==25354== by 0x3A05A478F4: g_malloc (in /lib64/libglib-2.0.so.0.2600.0) ==25354== by 0x3A0362FFEF: g_process_set_argv_space (gprocess.c:502) ==25354== by 0x40164C: main (main.c:196) The memory leak problem which resolved by patch 2 is very clear. The only concern from me is patch 1, would it cause any side effect? Thanks!
Otherwise we have no chance to free this stuff. Signed-off-by: Xufeng Zhang <xufeng.zhang@windriver.com> --- lib/logwriter.c | 17 +++++++++++++++-- 1 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/logwriter.c b/lib/logwriter.c index 3292e31..c4d0fdf 100644 --- a/lib/logwriter.c +++ b/lib/logwriter.c @@ -33,6 +33,7 @@ #include "mainloop-call.h" #include "ml-batched-timer.h" #include "str-format.h" +#include "logproto-text-client.h" #include <unistd.h> #include <assert.h> @@ -1006,6 +1007,7 @@ log_writer_flush(LogWriter *self, LogWriterFlushMode flush_mode) gint count = 0; gboolean ignore_throttle = (flush_mode >= LW_FLUSH_QUEUE); LogProtoStatus status = LPS_SUCCESS; + LogProtoTextClient *self_text; if (!proto) return FALSE; @@ -1063,8 +1065,19 @@ log_writer_flush(LogWriter *self, LogWriterFlushMode flush_mode) } else { - /* push back to the queue */ - log_queue_push_head(self->queue, lm, &path_options); + self_text = (LogProtoTextClient *) proto; + /* free the unconsumed message during configuration reloading */ + if ((LW_FLUSH_QUEUE == flush_mode) && self_text->partial_free && self_text->partial) + { + self_text->partial_free(self_text->partial); + self_text->partial = NULL; + log_msg_unref(lm); + } + else + { + /* push back to the queue */ + log_queue_push_head(self->queue, lm, &path_options); + } msg_set_context(NULL); log_msg_refcache_stop(); break; -- 1.7.0.2
Some mutexes are initialized but are not freed. Signed-off-by: Xufeng Zhang <xufeng.zhang@windriver.com> --- lib/logqueue.c | 1 + modules/affile/affile-dest.c | 2 ++ modules/dbparser/dbparser.c | 1 + 3 files changed, 4 insertions(+), 0 deletions(-) diff --git a/lib/logqueue.c b/lib/logqueue.c index 6c4e24f..118a11f 100644 --- a/lib/logqueue.c +++ b/lib/logqueue.c @@ -188,6 +188,7 @@ log_queue_init_instance(LogQueue *self, const gchar *persist_name) void log_queue_free_method(LogQueue *self) { + g_static_mutex_free(&self->lock); g_free(self->persist_name); g_free(self); } diff --git a/modules/affile/affile-dest.c b/modules/affile/affile-dest.c index f1be51e..e5de7f0 100644 --- a/modules/affile/affile-dest.c +++ b/modules/affile/affile-dest.c @@ -317,6 +317,7 @@ affile_dw_free(LogPipe *s) { AFFileDestWriter *self = (AFFileDestWriter *) s; + g_static_mutex_free(&self->lock); log_pipe_unref((LogPipe *) self->writer); self->writer = NULL; g_free(self->filename); @@ -698,6 +699,7 @@ affile_dd_free(LogPipe *s) /* NOTE: this must be NULL as deinit has freed it, otherwise we'd have circular references */ g_assert(self->single_writer == NULL && self->writer_hash == NULL); + g_static_mutex_free(&self->lock); log_template_unref(self->filename_template); log_writer_options_destroy(&self->writer_options); log_dest_driver_free(s); diff --git a/modules/dbparser/dbparser.c b/modules/dbparser/dbparser.c index d71c659..6b87226 100644 --- a/modules/dbparser/dbparser.c +++ b/modules/dbparser/dbparser.c @@ -287,6 +287,7 @@ log_db_parser_free(LogPipe *s) { LogDBParser *self = (LogDBParser *) s; + g_static_mutex_free(&self->lock); if (self->db) pattern_db_free(self->db); -- 1.7.0.2
These memories are allocated in g_process_set_argv_space() when HAVE_ENVIRON is defined, but are not freed anywhere. Signed-off-by: Xufeng Zhang <xufeng.zhang@windriver.com> --- lib/gprocess.c | 13 +++++++++++++ 1 files changed, 13 insertions(+), 0 deletions(-) diff --git a/lib/gprocess.c b/lib/gprocess.c index 9053bc9..6cc0b1f 100644 --- a/lib/gprocess.c +++ b/lib/gprocess.c @@ -1433,6 +1433,19 @@ g_process_startup_ok(void) void g_process_finish(void) { +#ifdef HAVE_ENVIRON + int i = 0; + + while (environ[i]) + { + g_free(environ[i]); + ++i; + } + if (environ) + g_free(environ); + if (process_opts.argv_orig) + free(process_opts.argv_orig); +#endif g_process_remove_pidfile(); } -- 1.7.0.2
participants (1)
-
Xufeng Zhang