diff --git a/cdr/cdr_odbc.c b/cdr/cdr_odbc.c
index f916a8d85234bfca7651709ad7ddb00567a51409..79ea1453af3a8ad13b6edd7a67e221e357d8e00f 100644
--- a/cdr/cdr_odbc.c
+++ b/cdr/cdr_odbc.c
@@ -346,7 +346,6 @@ static int odbc_load_module(void)
 		goto out;
 	}
 
-	ast_config_destroy(cfg);
 	if (option_verbose > 2) {
 		ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: dsn is %s\n",dsn);
 		if (username)
@@ -371,6 +370,8 @@ static int odbc_load_module(void)
 		ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n");
 	}
 out:
+	if (cfg)
+		ast_config_destroy(cfg);
 	ast_mutex_unlock(&odbc_lock);
 	return res;
 }