diff --git a/funcs/func_odbc.c b/funcs/func_odbc.c index e23dff5f21..d56a1df276 100644 --- a/funcs/func_odbc.c +++ b/funcs/func_odbc.c @@ -338,8 +338,10 @@ static int acf_odbc_write(struct ast_channel *chan, const char *cmd, char *s, co 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 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha 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 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha 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 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha 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 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha 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 @@ end_acf_read: 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 @@ end_acf_read: 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 @@ static char *cli_odbc_read(struct ast_cli_entry *e, int cmd, struct ast_cli_args 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 @@ static char *cli_odbc_read(struct ast_cli_entry *e, int cmd, struct ast_cli_args 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 @@ static char *cli_odbc_read(struct ast_cli_entry *e, int cmd, struct ast_cli_args 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 @@ static char *cli_odbc_read(struct ast_cli_entry *e, int cmd, struct ast_cli_args 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 @@ static char *cli_odbc_write(struct ast_cli_entry *e, int cmd, struct ast_cli_arg } 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 @@ static char *cli_odbc_write(struct ast_cli_entry *e, int cmd, struct ast_cli_arg 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; diff --git a/res/res_odbc.c b/res/res_odbc.c index 3a9b8eea64..ee5cc303d8 100644 --- a/res/res_odbc.c +++ b/res/res_odbc.c @@ -100,9 +100,11 @@ static int null_hash_fn(const void *obj, const int flags) 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 @@ static char *handle_cli_odbc_show(struct ast_cli_entry *e, int cmd, struct ast_c 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); - break; } ao2_ref(class, -1); + if (ret) { + break; + } } if (!ret && !strncasecmp(a->word, "all", length) && ++which > a->n) { ret = ast_strdup("all"); @@ -691,6 +694,8 @@ struct odbc_obj *ast_odbc_request_obj(const char *name, int check) 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 @@ struct odbc_obj *ast_odbc_request_obj(const char *name, int check) 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 @@ struct odbc_obj *ast_odbc_request_obj(const char *name, int check) 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 @@ struct odbc_obj *ast_odbc_request_obj(const char *name, int check) 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 @@ struct odbc_obj *ast_odbc_request_obj(const char *name, int check) obj = NULL; } else { ao2_link(class->obj_container, obj); + ast_assert(ao2_ref(obj, 0) > 1); } class = NULL; } @@ -775,6 +789,9 @@ struct odbc_obj *ast_odbc_request_obj(const char *name, int check) #endif ast_assert(class == NULL); + if (obj) { + ast_assert(ao2_ref(obj, 0) > 1); + } return obj; } @@ -794,10 +811,12 @@ static odbc_status odbc_obj_disconnect(struct odbc_obj *obj) 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)) {