diff --git a/libavutil/eval.c b/libavutil/eval.c
index 4d55f42664ebe0da7f137d1cb7c024da504627c1..9bc40c1edca23e1a576ca73980acfdd42397051d 100644
--- a/libavutil/eval.c
+++ b/libavutil/eval.c
@@ -489,6 +489,7 @@ int av_expr_parse(AVExpr **expr, const char *s,
     if ((ret = parse_expr(&e, &p)) < 0)
         goto end;
     if (*p.s) {
+        av_expr_free(e);
         av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0);
         ret = AVERROR(EINVAL);
         goto end;