From a41fa2e2bc45d2e9b975cb89d8639d2f6b0a54bd Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthony.minessale@gmail.com>
Date: Tue, 6 May 2008 22:56:55 +0000
Subject: [PATCH] fix possible register memory leak

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@8281 d0543943-73ff-0310-b7d9-9358b9ac24b2
---
 src/mod/endpoints/mod_sofia/mod_sofia.c      |  1 +
 src/mod/endpoints/mod_sofia/mod_sofia.h      |  2 ++
 src/mod/endpoints/mod_sofia/sofia.c          |  7 ++++++-
 src/mod/endpoints/mod_sofia/sofia_presence.c | 10 +++++++---
 4 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.c b/src/mod/endpoints/mod_sofia/mod_sofia.c
index 00bb5d7c2d..6525d06ce0 100644
--- a/src/mod/endpoints/mod_sofia/mod_sofia.c
+++ b/src/mod/endpoints/mod_sofia/mod_sofia.c
@@ -1899,6 +1899,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_sofia_load)
 	silence_frame.flags = SFF_CNG;
 
 	memset(&mod_sofia_globals, 0, sizeof(mod_sofia_globals));
+	mod_sofia_globals.destroy_private.destroy_nh = 1;
 	mod_sofia_globals.pool = pool;
 	switch_mutex_init(&mod_sofia_globals.mutex, SWITCH_MUTEX_NESTED, mod_sofia_globals.pool);
 
diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.h b/src/mod/endpoints/mod_sofia/mod_sofia.h
index f672730b43..3d06027f31 100644
--- a/src/mod/endpoints/mod_sofia/mod_sofia.h
+++ b/src/mod/endpoints/mod_sofia/mod_sofia.h
@@ -100,6 +100,7 @@ struct sofia_private {
 	char uuid[SWITCH_UUID_FORMATTED_LENGTH + 1];
 	sofia_gateway_t *gateway;
 	char gateway_name[512];
+	int destroy_nh;
 };
 
 #define set_param(ptr,val) if (ptr) {free(ptr) ; ptr = NULL;} if (val) {ptr = strdup(val);}
@@ -194,6 +195,7 @@ struct mod_sofia_globals {
 	char guess_ip[80];
 	switch_queue_t *presence_queue;
 	switch_queue_t *mwi_queue;
+	struct sofia_private destroy_private;
 };
 extern struct mod_sofia_globals mod_sofia_globals;
 
diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c
index 5b9b3931e6..2165d49b8d 100644
--- a/src/mod/endpoints/mod_sofia/sofia.c
+++ b/src/mod/endpoints/mod_sofia/sofia.c
@@ -194,7 +194,7 @@ void sofia_event_callback(nua_event_t event,
 	switch_channel_t *channel = NULL;
 	sofia_gateway_t *gateway = NULL;
 
-	if (sofia_private) {
+	if (sofia_private && sofia_private != &mod_sofia_globals.destroy_private) {
 		if ((gateway = sofia_private->gateway)) {
 			if (switch_thread_rwlock_tryrdlock(gateway->profile->rwlock) != SWITCH_STATUS_SUCCESS) {
 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is locked\n", gateway->profile->name);
@@ -356,6 +356,11 @@ void sofia_event_callback(nua_event_t event,
 
   done:
 
+	if (sofia_private && sofia_private->destroy_nh) {
+		//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Destroy handle requested.\n");
+		nua_handle_destroy(nh);
+	}
+
 	if (gateway) {
 		sofia_reg_release_gateway(gateway);
 	}
diff --git a/src/mod/endpoints/mod_sofia/sofia_presence.c b/src/mod/endpoints/mod_sofia/sofia_presence.c
index 773c36d299..de08c1591b 100644
--- a/src/mod/endpoints/mod_sofia/sofia_presence.c
+++ b/src/mod/endpoints/mod_sofia/sofia_presence.c
@@ -781,7 +781,8 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
 			exptime = tmp - switch_timestamp(NULL); 
 		}
 	}
-
+	
+	//if (!(nh = nua_handle_by_call_id(profile->nua, call_id))) {
 	if (!(nh = (nua_handle_t *) switch_core_hash_find(profile->sub_hash, call_id))) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot find handle for %s\n", call_id);
 		return 0;
@@ -1045,7 +1046,7 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
 	switch_snprintf(exp, sizeof(exp), "active;expires=%ld", (long) exptime); 
 
 	nua_notify(nh,
-			   NUTAG_NEWSUB(1),
+			   //NUTAG_NEWSUB(1),
 			   SIPTAG_SUBSCRIPTION_STATE_STR(exp),
 			   SIPTAG_EVENT_STR(event), SIPTAG_CONTENT_TYPE_STR(ct), SIPTAG_PAYLOAD_STR(pl), TAG_END());
 
@@ -1090,6 +1091,7 @@ static int sofia_presence_mwi_callback(void *pArg, int argc, char **argv, char *
 		return 0;
 	}
 	
+	//if (!(nh = nua_handle_by_call_id(profile->nua, call_id))) {
 	if (!(nh = (nua_handle_t *) switch_core_hash_find(profile->sub_hash, call_id))) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot find handle for %s\n", call_id);
 		return 0;
@@ -1103,7 +1105,7 @@ static int sofia_presence_mwi_callback(void *pArg, int argc, char **argv, char *
 	exp = switch_mprintf("active;expires=%ld", expire_sec);
 
 	nua_notify(nh,
-			   NUTAG_NEWSUB(1),
+			   //NUTAG_NEWSUB(1),
 			   SIPTAG_SUBSCRIPTION_STATE_STR(exp),
 			   SIPTAG_EVENT_STR(event), SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"), SIPTAG_PAYLOAD_STR(body), TAG_END());
 	
@@ -1146,6 +1148,7 @@ static int sofia_presence_mwi_callback2(void *pArg, int argc, char **argv, char
 					SIPTAG_CONTACT_STR(profile->url),
 					TAG_END());
 	
+	nua_handle_bind(nh, &mod_sofia_globals.destroy_private);
 	nua_notify(nh,
 			   NUTAG_NEWSUB(1),
 			   SIPTAG_EVENT_STR(event), SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"), SIPTAG_PAYLOAD_STR(body), TAG_END());
@@ -1347,6 +1350,7 @@ void sofia_presence_handle_sip_i_subscribe(int status,
 		sent_reply++;
 
 #if 0
+		nua_handle_bind(nh, &mod_sofia_globals.destroy_private);
 		nua_notify(nh, 
 				   NUTAG_NEWSUB(1),
 				   SIPTAG_SUBSCRIPTION_STATE_STR(sstr), SIPTAG_EVENT_STR(event),