From 9653b9438731d92f82458156f34969d9251ef500 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Mon, 31 Mar 2025 13:19:26 +0100
Subject: [PATCH] check-va-api: Pair calls to vaDestroySurfaces() with
 create_surfaces()

Each test creates new surfaces and puts their IDs in the array `surfaces`,
and previously we only destroyed the surfaces created by the last test.
The result is that if we ran more than one test, the surfaces created
by earlier tests would be leaked.

For example, if the VA-API implementation does not support H264 but
does support MPEG, we would have leaked the pair of surfaces that were
created in order to test H264, but we would correctly destroy the pair
of surfaces that were created in order to test MPEG. Now we destroy both.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 helpers/check-va-api.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/helpers/check-va-api.c b/helpers/check-va-api.c
index 46a4155e..671c7c82 100644
--- a/helpers/check-va-api.c
+++ b/helpers/check-va-api.c
@@ -52,6 +52,18 @@ clear_va_display (VADisplay *displayp)
   clear_pointer (displayp, vaTerminate);
 }
 
+static void
+clear_surfaces (VADisplay va_display,
+                VASurfaceID *surfaces,
+                int surfaces_count)
+{
+  if (va_display && surfaces && surfaces[0] != VA_INVALID_SURFACE)
+    vaDestroySurfaces (va_display, surfaces, surfaces_count);
+
+  for (int i = 0; i < surfaces_count; i++)
+    surfaces[i] = VA_INVALID_SURFACE;
+}
+
 enum
 {
   OPTION_HELP = 1,
@@ -443,6 +455,8 @@ out:
   if (slice_data_buf != VA_INVALID_ID)
     vaDestroyBuffer (va_display, slice_data_buf);
 
+  clear_surfaces (va_display, surfaces, surfaces_count);
+
   return ret;
 }
 
@@ -524,6 +538,8 @@ out:
   if (context != VA_INVALID_ID)
     vaDestroyContext (va_display, context);
 
+  clear_surfaces (va_display, surfaces, surfaces_count);
+
   return ret;
 }
 
@@ -645,11 +661,5 @@ main (int argc,
     ret = 0;
 
 out:
-  if (va_display)
-    {
-      if (surfaces && surfaces[0] != VA_INVALID_SURFACE)
-        vaDestroySurfaces (va_display, surfaces, surfaces_count);
-    }
-
   return ret;
 }
-- 
GitLab