diff --git a/res/res_config_sqlite.c b/res/res_config_sqlite.c
index c1ee895bfe9d84505bfb0216743403b15bc9a22e..1dcf254e3184fde0fc8b6e1d494d672125462419 100644
--- a/res/res_config_sqlite.c
+++ b/res/res_config_sqlite.c
@@ -1091,6 +1091,7 @@ static struct ast_config *realtime_multi_handler(const char *database,
 
 	if (!(tmp_str = sqlite_mprintf("%s ORDER BY %q;", query, initfield))) {
 		ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
+		sqlite_freemem(query);
 		ast_config_destroy(cfg);
 		ast_free(initfield);
 		return NULL;