Fix memory leak am: 90f51e0c85 am: 2c81aa30c4 am: fdc8a37027

Original change: https://googleplex-android-review.googlesource.com/c/platform/test/vts-testcase/hal/+/12468983

Change-Id: I9a03b1e60bf853795abb256fa1584d726c4b6d9b
diff --git a/neuralnetworks/V1_2/benchmark/java/src/com/android/nn/benchmark/vts/v1_2/NNAccuracyTest.java b/neuralnetworks/V1_2/benchmark/java/src/com/android/nn/benchmark/vts/v1_2/NNAccuracyTest.java
index 17ca0ba..6c1aaf6 100644
--- a/neuralnetworks/V1_2/benchmark/java/src/com/android/nn/benchmark/vts/v1_2/NNAccuracyTest.java
+++ b/neuralnetworks/V1_2/benchmark/java/src/com/android/nn/benchmark/vts/v1_2/NNAccuracyTest.java
@@ -117,17 +117,18 @@
     @Test
     @LargeTest
     public void testDriver() throws BenchmarkException, IOException {
-        NNTestBase test = mModel.mEntry.createNNTestBase();
-        test.useNNApi();
-        test.setNNApiDeviceName(mModel.mInstance);
-        if (!test.setupModel(mActivity)) {
-            throw new AssumptionViolatedException("The driver rejected the model.");
+        try (NNTestBase test = mModel.mEntry.createNNTestBase()) {
+            test.useNNApi();
+            test.setNNApiDeviceName(mModel.mInstance);
+            if (!test.setupModel(mActivity)) {
+                throw new AssumptionViolatedException("The driver rejected the model.");
+            }
+            Pair<List<InferenceInOutSequence>, List<InferenceResult>> inferenceResults =
+                    test.runBenchmarkCompleteInputSet(/*setRepeat=*/1, /*timeoutSec=*/3600);
+            BenchmarkResult benchmarkResult = BenchmarkResult.fromInferenceResults(
+                    mModel.mEntry.mModelName, BenchmarkResult.BACKEND_TFLITE_NNAPI,
+                    inferenceResults.first, inferenceResults.second, test.getEvaluator());
+            assertFalse(benchmarkResult.hasValidationErrors());
         }
-        Pair<List<InferenceInOutSequence>, List<InferenceResult>> inferenceResults =
-                test.runBenchmarkCompleteInputSet(/*setRepeat=*/1, /*timeoutSec=*/3600);
-        BenchmarkResult benchmarkResult = BenchmarkResult.fromInferenceResults(
-                mModel.mEntry.mModelName, BenchmarkResult.BACKEND_TFLITE_NNAPI,
-                inferenceResults.first, inferenceResults.second, test.getEvaluator());
-        assertFalse(benchmarkResult.hasValidationErrors());
     }
 }
diff --git a/neuralnetworks/V1_3/benchmark/java/src/com/android/nn/benchmark/vts/v1_3/NNAccuracyTest.java b/neuralnetworks/V1_3/benchmark/java/src/com/android/nn/benchmark/vts/v1_3/NNAccuracyTest.java
index bdc4311..d4a05fc 100644
--- a/neuralnetworks/V1_3/benchmark/java/src/com/android/nn/benchmark/vts/v1_3/NNAccuracyTest.java
+++ b/neuralnetworks/V1_3/benchmark/java/src/com/android/nn/benchmark/vts/v1_3/NNAccuracyTest.java
@@ -117,17 +117,18 @@
     @Test
     @LargeTest
     public void testDriver() throws BenchmarkException, IOException {
-        NNTestBase test = mModel.mEntry.createNNTestBase();
-        test.useNNApi();
-        test.setNNApiDeviceName(mModel.mInstance);
-        if (!test.setupModel(mActivity)) {
-            throw new AssumptionViolatedException("The driver rejected the model.");
+        try (NNTestBase test = mModel.mEntry.createNNTestBase()) {
+            test.useNNApi();
+            test.setNNApiDeviceName(mModel.mInstance);
+            if (!test.setupModel(mActivity)) {
+                throw new AssumptionViolatedException("The driver rejected the model.");
+            }
+            Pair<List<InferenceInOutSequence>, List<InferenceResult>> inferenceResults =
+                    test.runBenchmarkCompleteInputSet(/*setRepeat=*/1, /*timeoutSec=*/3600);
+            BenchmarkResult benchmarkResult = BenchmarkResult.fromInferenceResults(
+                    mModel.mEntry.mModelName, BenchmarkResult.BACKEND_TFLITE_NNAPI,
+                    inferenceResults.first, inferenceResults.second, test.getEvaluator());
+            assertFalse(benchmarkResult.hasValidationErrors());
         }
-        Pair<List<InferenceInOutSequence>, List<InferenceResult>> inferenceResults =
-                test.runBenchmarkCompleteInputSet(/*setRepeat=*/1, /*timeoutSec=*/3600);
-        BenchmarkResult benchmarkResult = BenchmarkResult.fromInferenceResults(
-                mModel.mEntry.mModelName, BenchmarkResult.BACKEND_TFLITE_NNAPI,
-                inferenceResults.first, inferenceResults.second, test.getEvaluator());
-        assertFalse(benchmarkResult.hasValidationErrors());
     }
 }