From 272dd008d08b93c96cbab94fd2bcd607152681f7 Mon Sep 17 00:00:00 2001
From: Matthew Jordan <mjordan@digium.com>
Date: Wed, 12 Jun 2013 02:29:08 +0000
Subject: [PATCH] Fix memory leak while loading modules, adding formats, and
 destroying endpoints

This patch fixes three memory leaks
 * When we load a module with the LOAD_PRIORITY flag, we remove its entry from
   the load order list. Unfortunately, we don't free the memory associated with
   entry in the list. This patch corrects that and properly frees the memory
   for the module in the list.

 * When adding a custom format (such as SILK or CELT), the routine for adding
   the format was leaking a reference. RAII_VAR cleans this up properly.

 * We now de-ref the channel_snapshot appropriately when an endpoint is
   disposed of
........

Merged revisions 391489 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........

Merged revisions 391507 from http://svn.asterisk.org/svn/asterisk/branches/11


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@391521 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 main/endpoints.c | 4 ++++
 main/format.c    | 2 +-
 main/loader.c    | 2 ++
 3 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/main/endpoints.c b/main/endpoints.c
index a5d50cfde6..9f6d2c0621 100644
--- a/main/endpoints.c
+++ b/main/endpoints.c
@@ -116,6 +116,9 @@ static void endpoint_dtor(void *obj)
 	ao2_cleanup(endpoint->topic);
 	endpoint->topic = NULL;
 
+	ao2_cleanup(endpoint->channel_ids);
+	endpoint->channel_ids = NULL;
+
 	ast_string_field_free_memory(endpoint);
 }
 
@@ -357,6 +360,7 @@ struct ast_endpoint_snapshot *ast_endpoint_snapshot_create(
 		RAII_VAR(char *, channel_id, obj, ao2_cleanup);
 		snapshot->channel_ids[snapshot->num_channels++] = channel_id;
 	}
+	ao2_iterator_destroy(&i);
 
 	ao2_ref(snapshot, +1);
 	return snapshot;
diff --git a/main/format.c b/main/format.c
index 36aa534c00..5b8b8b05fe 100644
--- a/main/format.c
+++ b/main/format.c
@@ -912,7 +912,7 @@ static struct ast_cli_entry my_clis[] = {
 
 static int format_list_add_custom(struct ast_format_list *new)
 {
-	struct ast_format_list *entry;
+	RAII_VAR(struct ast_format_list *, entry, NULL, ao2_cleanup);
 	if (!(entry = ao2_alloc(sizeof(*entry), NULL))) {
 		return -1;
 	}
diff --git a/main/loader.c b/main/loader.c
index be08d7f962..86735df52e 100644
--- a/main/loader.c
+++ b/main/loader.c
@@ -1110,6 +1110,8 @@ static int load_resource_list(struct load_order *load_order, unsigned int global
 			break;
 		case AST_MODULE_LOAD_PRIORITY:
 			AST_LIST_REMOVE_CURRENT(entry);
+			ast_free(order->resource);
+			ast_free(order);
 			break;
 		}
 	}
-- 
GitLab