From: Automerge script Date: Sun, 4 Nov 2012 01:24:59 +0000 (+0000) Subject: Merged revisions 375759,375761 via svnmerge from X-Git-Tag: 10.11.0-digiumphones-rc1~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5d25d0b9cd64bb2c14d8fd6ec9e47680453e6f38;p=thirdparty%2Fasterisk.git Merged revisions 375759,375761 via svnmerge from file:///srv/subversion/repos/asterisk/branches/10 ................ r375759 | mjordan | 2012-11-03 19:55:19 -0500 (Sat, 03 Nov 2012) | 18 lines Fix memory leak when unloading XML documentation This patch is a modified version of a patch originally committed for the Asterisk 11 branch in r375756. A portion of that patch, that fixed the memory leak during unloading XML documentation, applies to branches 1.8 and 10 as well. The patch for this issue was modified for these two branches. (issue ASTERISK-20648) Reported by: Corey Farrell Tested by: mjordan patches: xmldoc-memory_leak.patch uploaded by Corey Farrell (license 5909) ........ Merged revisions 375758 from http://svn.asterisk.org/svn/asterisk/branches/1.8 ................ r375761 | mjordan | 2012-11-03 20:13:37 -0500 (Sat, 03 Nov 2012) | 15 lines Properly finalize prepared SQLite3 statements to prevent memory leak The AstDB uses prepared SQLite3 statements to retrieve data from the SQLite3 database. These statements should be finalized during Asterisk shutdown so that the SQLite3 database can be properly closed. Failure to finalize the statements results in a memory leak and a failure when closing the database. This patch fixes those issues by ensuring that all prepared statements are properly finalized at shutdown. (closes issue ASTERISK-20647) Reported by: Corey Farrell patches: astdb-sqlite3_close.patch uploaded by Corey Farrell (license 5909) ................ git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/10-digiumphones@375788 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- diff --git a/main/db.c b/main/db.c index 2ff2e7d2ec..4a53231632 100644 --- a/main/db.c +++ b/main/db.c @@ -141,6 +141,36 @@ static int init_stmt(sqlite3_stmt **stmt, const char *sql, size_t len) return 0; } +/*! \internal + * \brief Clean up the prepared SQLite3 statement + * \note dblock should already be locked prior to calling this method + */ +static int clean_stmt(sqlite3_stmt *stmt, const char *sql) +{ + if (sqlite3_finalize(stmt) != SQLITE_OK) { + ast_log(LOG_WARNING, "Couldn't finalize statement '%s': %s\n", sql, sqlite3_errmsg(astdb)); + return -1; + } + return 0; +} + +/*! \internal + * \brief Clean up all prepared SQLite3 statements + * \note dblock should already be locked prior to calling this method + */ +static void clean_statements(void) +{ + clean_stmt(get_stmt, get_stmt_sql); + clean_stmt(del_stmt, del_stmt_sql); + clean_stmt(deltree_stmt, deltree_stmt_sql); + clean_stmt(deltree_all_stmt, deltree_all_stmt_sql); + clean_stmt(gettree_stmt, gettree_stmt_sql); + clean_stmt(gettree_all_stmt, gettree_all_stmt_sql); + clean_stmt(showkey_stmt, showkey_stmt_sql); + clean_stmt(put_stmt, put_stmt_sql); + clean_stmt(create_astdb_stmt, create_astdb_stmt_sql); +} + static int init_statements(void) { /* Don't initialize create_astdb_statment here as the astdb table needs to exist @@ -955,6 +985,7 @@ static void *db_sync_thread(void *data) /*! \internal \brief Clean up resources on Asterisk shutdown */ static void astdb_atexit(void) { + ast_cli_unregister_multiple(cli_database, ARRAY_LEN(cli_database)); ast_manager_unregister("DBGet"); ast_manager_unregister("DBPut"); ast_manager_unregister("DBDel"); @@ -969,7 +1000,10 @@ static void astdb_atexit(void) pthread_join(syncthread, NULL); ast_mutex_lock(&dblock); - sqlite3_close(astdb); + clean_statements(); + if (sqlite3_close(astdb) == SQLITE_OK) { + astdb = NULL; + } ast_mutex_unlock(&dblock); } diff --git a/main/xmldoc.c b/main/xmldoc.c index cee70499a7..125cd240b9 100644 --- a/main/xmldoc.c +++ b/main/xmldoc.c @@ -1944,6 +1944,7 @@ static void xmldoc_unload_documentation(void) while ((doctree = AST_RWLIST_REMOVE_HEAD(&xmldoc_tree, entry))) { ast_free(doctree->filename); ast_xml_close(doctree->doc); + ast_free(doctree); } AST_RWLIST_UNLOCK(&xmldoc_tree);