[asterisk-commits] tilghman: trunk r176592 - in /trunk: funcs/func_odbc.c res/res_odbc.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Feb 17 12:49:20 CST 2009


Author: tilghman
Date: Tue Feb 17 12:49:20 2009
New Revision: 176592

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=176592
Log:
Add assertions in the quest to track down a refcount leak.
(closes issue #14485)
 Reported by: davevg

Modified:
    trunk/funcs/func_odbc.c
    trunk/res/res_odbc.c

Modified: trunk/funcs/func_odbc.c
URL: http://svn.digium.com/svn-view/asterisk/trunk/funcs/func_odbc.c?view=diff&rev=176592&r1=176591&r2=176592
==============================================================================
--- trunk/funcs/func_odbc.c (original)
+++ trunk/funcs/func_odbc.c Tue Feb 17 12:49:20 2009
@@ -338,8 +338,10 @@
 		SQLCloseCursor(stmt);
 		SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 	}
-	if (obj)
+	if (obj) {
 		ast_odbc_release_obj(obj);
+		obj = NULL;
+	}
 
 	if (chan)
 		ast_autoservice_stop(chan);
@@ -443,6 +445,7 @@
 		ast_log(LOG_ERROR, "Unable to execute query [%s]\n", ast_str_buffer(sql));
 		if (obj) {
 			ast_odbc_release_obj(obj);
+			obj = NULL;
 		}
 		pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
 		if (chan) {
@@ -460,6 +463,7 @@
 		SQLCloseCursor(stmt);
 		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
 		ast_odbc_release_obj(obj);
+		obj = NULL;
 		pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
 		if (chan) {
 			ast_autoservice_stop(chan);
@@ -486,6 +490,7 @@
 		SQLCloseCursor(stmt);
 		SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 		ast_odbc_release_obj(obj);
+		obj = NULL;
 		pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
 		pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
 		if (chan)
@@ -528,6 +533,7 @@
 						SQLCloseCursor(stmt);
 						SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 						ast_odbc_release_obj(obj);
+						obj = NULL;
 						pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
 						pbx_builtin_setvar_helper(chan, "ODBCSTATUS", "MEMERROR");
 						if (chan)
@@ -625,6 +631,7 @@
 			SQLCloseCursor(stmt);
 			SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 			ast_odbc_release_obj(obj);
+			obj = NULL;
 			if (chan)
 				ast_autoservice_stop(chan);
 			if (bogus_chan)
@@ -637,6 +644,7 @@
 	SQLCloseCursor(stmt);
 	SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 	ast_odbc_release_obj(obj);
+	obj = NULL;
 	if (chan)
 		ast_autoservice_stop(chan);
 	if (bogus_chan)
@@ -1056,6 +1064,7 @@
 			ast_debug(1, "Got obj\n");
 			if (!(stmt = ast_odbc_direct_execute(obj, generic_execute, ast_str_buffer(sql)))) {
 				ast_odbc_release_obj(obj);
+				obj = NULL;
 				continue;
 			}
 
@@ -1067,6 +1076,7 @@
 				SQLCloseCursor(stmt);
 				SQLFreeHandle (SQL_HANDLE_STMT, stmt);
 				ast_odbc_release_obj(obj);
+				obj = NULL;
 				AST_RWLIST_UNLOCK(&queries);
 				return CLI_SUCCESS;
 			}
@@ -1076,6 +1086,7 @@
 				SQLCloseCursor(stmt);
 				SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 				ast_odbc_release_obj(obj);
+				obj = NULL;
 				if (res == SQL_NO_DATA) {
 					ast_cli(a->fd, "Returned %d rows.  Query executed on handle %d:%s [%s]\n", rows, dsn, query->readhandle[dsn], ast_str_buffer(sql));
 					break;
@@ -1103,27 +1114,33 @@
 						SQLCloseCursor(stmt);
 						SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 						ast_odbc_release_obj(obj);
+						obj = NULL;
 						AST_RWLIST_UNLOCK(&queries);
 						return CLI_SUCCESS;
 					}
 
 					ast_cli(a->fd, "%-20.20s  %s\n", colname, ast_str_buffer(coldata));
 				}
+				rows++;
+
 				/* Get next row */
 				res = SQLFetch(stmt);
 				if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 					break;
 				}
 				ast_cli(a->fd, "%-20.20s  %s\n", "----------", "----------");
-				rows++;
 			}
 			SQLCloseCursor(stmt);
 			SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 			ast_odbc_release_obj(obj);
-			ast_cli(a->fd, "Returned %d rows.  Query executed on handle %d [%s]\n", rows, dsn, query->readhandle[dsn]);
+			obj = NULL;
+			ast_cli(a->fd, "Returned %d row%s.  Query executed on handle %d [%s]\n", rows, rows == 1 ? "" : "s", dsn, query->readhandle[dsn]);
 			break;
 		}
-		ast_odbc_release_obj(obj);
+		if (obj) {
+			ast_odbc_release_obj(obj);
+			obj = NULL;
+		}
 
 		if (!executed) {
 			ast_cli(a->fd, "Failed to execute query. [%s]\n", ast_str_buffer(sql));
@@ -1252,6 +1269,7 @@
 			}
 			if (!(stmt = ast_odbc_direct_execute(obj, generic_execute, ast_str_buffer(sql)))) {
 				ast_odbc_release_obj(obj);
+				obj = NULL;
 				continue;
 			}
 
@@ -1259,6 +1277,7 @@
 			SQLCloseCursor(stmt);
 			SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 			ast_odbc_release_obj(obj);
+			obj = NULL;
 			ast_cli(a->fd, "Affected %d rows.  Query executed on handle %d [%s]\n", (int)rows, dsn, query->writehandle[dsn]);
 			executed = 1;
 			break;

Modified: trunk/res/res_odbc.c
URL: http://svn.digium.com/svn-view/asterisk/trunk/res/res_odbc.c?view=diff&rev=176592&r1=176591&r2=176592
==============================================================================
--- trunk/res/res_odbc.c (original)
+++ trunk/res/res_odbc.c Tue Feb 17 12:49:20 2009
@@ -100,9 +100,11 @@
 static void odbc_obj_destructor(void *data)
 {
 	struct odbc_obj *obj = data;
+	struct odbc_class *class = obj->parent;
+	obj->parent = NULL;
 	odbc_obj_disconnect(obj);
 	ast_mutex_destroy(&obj->lock);
-	ao2_ref(obj->parent, -1);
+	ao2_ref(class, -1);
 }
 
 static void destroy_table_cache(struct odbc_cache_tables *table) {
@@ -573,10 +575,11 @@
 		while ((class = ao2_iterator_next(&aoi))) {
 			if (!strncasecmp(a->word, class->name, length) && ++which > a->n) {
 				ret = ast_strdup(class->name);
-				ao2_ref(class, -1);
+			}
+			ao2_ref(class, -1);
+			if (ret) {
 				break;
 			}
-			ao2_ref(class, -1);
 		}
 		if (!ret && !strncasecmp(a->word, "all", length) && ++which > a->n) {
 			ret = ast_strdup("all");
@@ -691,6 +694,8 @@
 	if (!class)
 		return NULL;
 
+	ast_assert(ao2_ref(class, 0) > 1);
+
 	if (class->haspool) {
 		/* Recycle connections before building another */
 		aoi = ao2_iterator_init(class->obj_container, 0);
@@ -704,6 +709,10 @@
 			ao2_ref(obj, -1);
 		}
 
+		if (obj) {
+			ast_assert(ao2_ref(obj, 0) > 1);
+		}
+
 		if (!obj && (class->count < class->limit)) {
 			class->count++;
 			obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor);
@@ -711,12 +720,14 @@
 				ao2_ref(class, -1);
 				return NULL;
 			}
+			ast_assert(ao2_ref(obj, 0) == 1);
 			ast_mutex_init(&obj->lock);
 			/* obj inherits the outstanding reference to class */
 			obj->parent = class;
 			if (odbc_obj_connect(obj) == ODBC_FAIL) {
 				ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
 				ao2_ref(obj, -1);
+				ast_assert(ao2_ref(class, 0) > 0);
 				obj = NULL;
 			} else {
 				obj->used = 1;
@@ -738,12 +749,14 @@
 
 		if (obj) {
 			/* Object is not constructed, so delete outstanding reference to class. */
+			ast_assert(ao2_ref(class, 0) > 1);
 			ao2_ref(class, -1);
 			class = NULL;
 		} else {
 			/* No entry: build one */
 			obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor);
 			if (!obj) {
+				ast_assert(ao2_ref(class, 0) > 1);
 				ao2_ref(class, -1);
 				return NULL;
 			}
@@ -756,6 +769,7 @@
 				obj = NULL;
 			} else {
 				ao2_link(class->obj_container, obj);
+				ast_assert(ao2_ref(obj, 0) > 1);
 			}
 			class = NULL;
 		}
@@ -775,6 +789,9 @@
 #endif
 	ast_assert(class == NULL);
 
+	if (obj) {
+		ast_assert(ao2_ref(obj, 0) > 1);
+	}
 	return obj;
 }
 
@@ -794,10 +811,12 @@
 
 	res = SQLDisconnect(obj->con);
 
-	if (res == SQL_SUCCESS || res == SQL_SUCCESS_WITH_INFO) {
-		ast_log(LOG_DEBUG, "Disconnected %d from %s [%s]\n", res, obj->parent->name, obj->parent->dsn);
-	} else {
-		ast_log(LOG_DEBUG, "res_odbc: %s [%s] already disconnected\n", obj->parent->name, obj->parent->dsn);
+	if (obj->parent) {
+		if (res == SQL_SUCCESS || res == SQL_SUCCESS_WITH_INFO) {
+			ast_log(LOG_DEBUG, "Disconnected %d from %s [%s]\n", res, obj->parent->name, obj->parent->dsn);
+		} else {
+			ast_log(LOG_DEBUG, "res_odbc: %s [%s] already disconnected\n", obj->parent->name, obj->parent->dsn);
+		}
 	}
 
 	if ((res = SQLFreeHandle(SQL_HANDLE_DBC, obj->con) == SQL_SUCCESS)) {




More information about the asterisk-commits mailing list