Fix Minor memory leaks in test_buffer/compiler. (#1160)

* Fix Minor memory leaks in test_buffer/compiler.

Signed-off-by: John Kesapides <john.kesapides@arm.com>

* Fixes in test_buffer
* Remove commented unmap call
* remove unused ii variables.

Signed-off-by: John Kesapides <john.kesapides@arm.com>

* test_buffer fixes:
* test_buffer_fill remove unsued ii

Signed-off-by: John Kesapides <john.kesapides@arm.com>
diff --git a/test_conformance/buffers/test_buffer_fill.cpp b/test_conformance/buffers/test_buffer_fill.cpp
index 8e0b93f..3edfafb 100644
--- a/test_conformance/buffers/test_buffer_fill.cpp
+++ b/test_conformance/buffers/test_buffer_fill.cpp
@@ -562,14 +562,13 @@
                      int loops, void *inptr[5], void *hostptr[5], void *pattern[5], size_t offset_elements, size_t fill_elements,
                      const char *kernelCode[], const char *kernelName[], int (*fn)(void *,void *,int) )
 {
-    clMemWrapper buffers[10];
     void        *outptr[5];
     clProgramWrapper program[5];
     clKernelWrapper kernel[5];
     size_t      ptrSizes[5];
     size_t      global_work_size[3];
     int         err;
-    int         i, ii;
+    int i;
     int         src_flag_id;
     int         total_errors = 0;
 
@@ -586,8 +585,6 @@
     loops = (loops < 5 ? loops : 5);
     for (i = 0; i < loops; i++)
     {
-        ii = i << 1;
-
         err = create_single_kernel_helper(context, &program[i], &kernel[i], 1,
                                           &kernelCode[i], kernelName[i]);
         if (err)
@@ -599,18 +596,25 @@
         for (src_flag_id = 0; src_flag_id < NUM_FLAGS; src_flag_id++)
         {
             clEventWrapper event[2];
-
+            clMemWrapper buffers[2];
             if ((flag_set[src_flag_id] & CL_MEM_USE_HOST_PTR) || (flag_set[src_flag_id] & CL_MEM_COPY_HOST_PTR))
-                buffers[ii] = clCreateBuffer(context, flag_set[src_flag_id],  ptrSizes[i] * num_elements, hostptr[i], &err);
+                buffers[0] = clCreateBuffer(context, flag_set[src_flag_id],
+                                            ptrSizes[i] * num_elements,
+                                            hostptr[i], &err);
             else
-                buffers[ii] = clCreateBuffer(context, flag_set[src_flag_id],  ptrSizes[i] * num_elements, NULL, &err);
-            if ( !buffers[ii] || err){
+                buffers[0] =
+                    clCreateBuffer(context, flag_set[src_flag_id],
+                                   ptrSizes[i] * num_elements, NULL, &err);
+            if (!buffers[0] || err)
+            {
                 print_error(err, "clCreateBuffer failed\n" );
                 return -1;
             }
             // Initialize source buffer with 0, since the validation code expects 0(s) outside of the fill region.
             if (!((flag_set[src_flag_id] & CL_MEM_USE_HOST_PTR) || (flag_set[src_flag_id] & CL_MEM_COPY_HOST_PTR))) {
-                err = clEnqueueWriteBuffer(queue, buffers[ii], CL_FALSE, 0, ptrSizes[i]*num_elements, hostptr[i], 0, NULL, NULL);
+                err = clEnqueueWriteBuffer(queue, buffers[0], CL_FALSE, 0,
+                                           ptrSizes[i] * num_elements,
+                                           hostptr[i], 0, NULL, NULL);
                 if ( err != CL_SUCCESS ){
                     print_error(err, "clEnqueueWriteBuffer failed\n" );
                     return -1;
@@ -619,27 +623,31 @@
 
             outptr[i] = align_malloc( ptrSizes[i] * num_elements, min_alignment);
             memset(outptr[i], 0, ptrSizes[i] * num_elements);
-            buffers[ii+1] = clCreateBuffer(context, CL_MEM_COPY_HOST_PTR,  ptrSizes[i] * num_elements, outptr[i], &err);
-            if ( !buffers[ii+1] || err){
+            buffers[1] =
+                clCreateBuffer(context, CL_MEM_COPY_HOST_PTR,
+                               ptrSizes[i] * num_elements, outptr[i], &err);
+            if (!buffers[1] || err)
+            {
                 print_error(err, "clCreateBuffer failed\n" );
                 align_free( outptr[i] );
                 return -1;
             }
 
-            err = clEnqueueFillBuffer(queue, buffers[ii], pattern[i], ptrSizes[i],
-                                      ptrSizes[i] * offset_elements, ptrSizes[i] * fill_elements,
-                                      0, NULL, &(event[0]));
-            /* uncomment for test debugging
-             err = clEnqueueWriteBuffer(queue, buffers[ii], CL_FALSE, 0, ptrSizes[i]*num_elements, inptr[i], 0, NULL, &(event[0]));
-             */
+            err = clEnqueueFillBuffer(
+                queue, buffers[0], pattern[i], ptrSizes[i],
+                ptrSizes[i] * offset_elements, ptrSizes[i] * fill_elements, 0,
+                NULL, &(event[0]));
+
             if ( err != CL_SUCCESS ){
                 print_error( err, " clEnqueueFillBuffer failed" );
                 align_free( outptr[i] );
                 return -1;
             }
 
-            err = clSetKernelArg( kernel[i], 0, sizeof( cl_mem ), (void *)&buffers[ii] );
-            err |= clSetKernelArg( kernel[i], 1, sizeof( cl_mem ), (void *)&buffers[ii+1] );
+            err = clSetKernelArg(kernel[i], 0, sizeof(cl_mem),
+                                 (void *)&buffers[0]);
+            err |= clSetKernelArg(kernel[i], 1, sizeof(cl_mem),
+                                  (void *)&buffers[1]);
             if ( err != CL_SUCCESS ){
                 print_error( err, "clSetKernelArg failed" );
                 align_free( outptr[i] );
@@ -659,7 +667,9 @@
                 return -1;
             }
 
-            err = clEnqueueReadBuffer( queue, buffers[ii+1], false, 0, ptrSizes[i]*num_elements, outptr[i], 0, NULL, &(event[1]) );
+            err = clEnqueueReadBuffer(queue, buffers[1], false, 0,
+                                      ptrSizes[i] * num_elements, outptr[i], 0,
+                                      NULL, &(event[1]));
             if (err != CL_SUCCESS){
                 print_error( err, "clEnqueueReadBuffer failed" );
                 return -1;
diff --git a/test_conformance/buffers/test_buffer_write.cpp b/test_conformance/buffers/test_buffer_write.cpp
index 2497dd1..e57e1c1 100644
--- a/test_conformance/buffers/test_buffer_write.cpp
+++ b/test_conformance/buffers/test_buffer_write.cpp
@@ -624,14 +624,13 @@
 int test_buffer_write( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements, size_t size, char *type, int loops,
                        void *inptr[5], const char *kernelCode[], const char *kernelName[], int (*fn)(void *,void *,int), MTdata d )
 {
-    clMemWrapper buffers[10];
     void        *outptr[5];
     clProgramWrapper program[5];
     clKernelWrapper kernel[5];
     size_t      ptrSizes[5];
     size_t      global_work_size[3];
     cl_int      err;
-    int         i, ii;
+    int i;
     int         src_flag_id, dst_flag_id;
     int         total_errors = 0;
 
@@ -660,13 +659,19 @@
         {
             for (dst_flag_id = 0; dst_flag_id < NUM_FLAGS; dst_flag_id++)
             {
-                ii = i << 1;
-                if ((flag_set[src_flag_id] & CL_MEM_USE_HOST_PTR) || (flag_set[src_flag_id] & CL_MEM_COPY_HOST_PTR))
-                    buffers[ii] = clCreateBuffer(context, flag_set[src_flag_id],  ptrSizes[i] * num_elements, inptr[i], &err);
-                else
-                    buffers[ii] = clCreateBuffer(context, flag_set[src_flag_id],  ptrSizes[i] * num_elements, NULL, &err);
+                clMemWrapper buffers[2];
 
-                if ( ! buffers[ii] || err){
+                if ((flag_set[src_flag_id] & CL_MEM_USE_HOST_PTR) || (flag_set[src_flag_id] & CL_MEM_COPY_HOST_PTR))
+                    buffers[0] = clCreateBuffer(context, flag_set[src_flag_id],
+                                                ptrSizes[i] * num_elements,
+                                                inptr[i], &err);
+                else
+                    buffers[0] =
+                        clCreateBuffer(context, flag_set[src_flag_id],
+                                       ptrSizes[i] * num_elements, NULL, &err);
+
+                if (!buffers[0] || err)
+                {
                     align_free( outptr[i] );
                     print_error(err, " clCreateBuffer failed\n" );
                     return -1;
@@ -674,19 +679,26 @@
                 if ( ! strcmp( type, "half" ) ){
                     outptr[i] = align_malloc( ptrSizes[i] * (num_elements * 2 ), min_alignment);
                     if ((flag_set[dst_flag_id] & CL_MEM_USE_HOST_PTR) || (flag_set[dst_flag_id] & CL_MEM_COPY_HOST_PTR))
-                        buffers[ii+1] = clCreateBuffer(context, flag_set[dst_flag_id],  ptrSizes[i] * 2 * num_elements, outptr[i], &err);
+                        buffers[1] = clCreateBuffer(
+                            context, flag_set[dst_flag_id],
+                            ptrSizes[i] * 2 * num_elements, outptr[i], &err);
                     else
-                        buffers[ii+1] = clCreateBuffer(context, flag_set[dst_flag_id],  ptrSizes[i] * 2 * num_elements, NULL, &err);
+                        buffers[1] = clCreateBuffer(
+                            context, flag_set[dst_flag_id],
+                            ptrSizes[i] * 2 * num_elements, NULL, &err);
                 }
                 else{
                     outptr[i] = align_malloc( ptrSizes[i] * num_elements, min_alignment);
                     if ((flag_set[dst_flag_id] & CL_MEM_USE_HOST_PTR) || (flag_set[dst_flag_id] & CL_MEM_COPY_HOST_PTR))
-                        buffers[ii+1] = clCreateBuffer(context, flag_set[dst_flag_id],  ptrSizes[i] * num_elements, outptr[i], &err);
+                        buffers[1] = clCreateBuffer(
+                            context, flag_set[dst_flag_id],
+                            ptrSizes[i] * num_elements, outptr[i], &err);
                     else
-                        buffers[ii+1] = clCreateBuffer(context, flag_set[dst_flag_id],  ptrSizes[i] * num_elements, NULL, &err);
+                        buffers[1] = clCreateBuffer(
+                            context, flag_set[dst_flag_id],
+                            ptrSizes[i] * num_elements, NULL, &err);
                 }
                 if ( err ){
-                    clReleaseMemObject(buffers[ii]);
                     align_free( outptr[i] );
                     print_error(err, " clCreateBuffer failed\n" );
                     return -1;
@@ -694,7 +706,9 @@
 
                 if (gTestMap) {
                     void *dataPtr;
-                    dataPtr = clEnqueueMapBuffer(queue, buffers[ii], CL_TRUE, CL_MAP_WRITE, 0, ptrSizes[i]*num_elements, 0, NULL, NULL, &err);
+                    dataPtr = clEnqueueMapBuffer(
+                        queue, buffers[0], CL_TRUE, CL_MAP_WRITE, 0,
+                        ptrSizes[i] * num_elements, 0, NULL, NULL, &err);
                     if (err) {
                         print_error(err, "clEnqueueMapBuffer failed");
                         align_free( outptr[i] );
@@ -703,7 +717,8 @@
 
                     memcpy(dataPtr, inptr[i], ptrSizes[i]*num_elements);
 
-                    err = clEnqueueUnmapMemObject(queue, buffers[ii], dataPtr, 0, NULL, NULL);
+                    err = clEnqueueUnmapMemObject(queue, buffers[0], dataPtr, 0,
+                                                  NULL, NULL);
                     if (err) {
                         print_error(err, "clEnqueueUnmapMemObject failed");
                         align_free( outptr[i] );
@@ -711,7 +726,9 @@
                     }
                 }
                 else if (!(flag_set[src_flag_id] & CL_MEM_USE_HOST_PTR) && !(flag_set[src_flag_id] & CL_MEM_COPY_HOST_PTR)) {
-                    err = clEnqueueWriteBuffer(queue, buffers[ii], CL_TRUE, 0, ptrSizes[i]*num_elements, inptr[i], 0, NULL, NULL);
+                    err = clEnqueueWriteBuffer(queue, buffers[0], CL_TRUE, 0,
+                                               ptrSizes[i] * num_elements,
+                                               inptr[i], 0, NULL, NULL);
                     if ( err != CL_SUCCESS ){
                         align_free( outptr[i] );
                         print_error( err, " clWriteBuffer failed" );
@@ -719,8 +736,10 @@
                     }
                 }
 
-                err = clSetKernelArg( kernel[i], 0, sizeof( cl_mem ), (void *)&buffers[ii] );
-                err |= clSetKernelArg( kernel[i], 1, sizeof( cl_mem ), (void *)&buffers[ii+1] );
+                err = clSetKernelArg(kernel[i], 0, sizeof(cl_mem),
+                                     (void *)&buffers[0]);
+                err |= clSetKernelArg(kernel[i], 1, sizeof(cl_mem),
+                                      (void *)&buffers[1]);
                 if ( err != CL_SUCCESS ){
                     align_free( outptr[i] );
                     print_error( err, " clSetKernelArg failed" );
@@ -734,12 +753,10 @@
                     return -1;
                 }
 
-                if ( ! strcmp( type, "half" ) ){
-                    err = clEnqueueReadBuffer( queue, buffers[ii+1], true, 0, ptrSizes[i]*num_elements, outptr[i], 0, NULL, NULL );
-                }
-                else{
-                    err = clEnqueueReadBuffer( queue, buffers[ii+1], true, 0, ptrSizes[i]*num_elements, outptr[i], 0, NULL, NULL );
-                }
+                err = clEnqueueReadBuffer(queue, buffers[1], true, 0,
+                                          ptrSizes[i] * num_elements, outptr[i],
+                                          0, NULL, NULL);
+
                 if ( err != CL_SUCCESS ){
                     align_free( outptr[i] );
                     print_error( err, " clEnqueueReadBuffer failed" );
@@ -774,7 +791,7 @@
 
 int test_buffer_write_struct( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements )
 {
-    clMemWrapper buffers[10];
+
     void        *outptr[5];
     TestStruct  *inptr[5];
     clProgramWrapper program[5];
@@ -783,7 +800,7 @@
     size_t      size = sizeof( TestStruct );
     size_t      global_work_size[3];
     cl_int      err;
-    int         i, ii;
+    int i;
     cl_uint     j;
     int         loops = 1;      // no vector for structs
     int         src_flag_id, dst_flag_id;
@@ -818,6 +835,7 @@
         {
             for (dst_flag_id = 0; dst_flag_id < NUM_FLAGS; dst_flag_id++)
             {
+                clMemWrapper buffers[2];
 
                 inptr[i] = (TestStruct *)align_malloc(ptrSizes[i] * num_elements, min_alignment);
 
@@ -826,11 +844,14 @@
                     inptr[i][j].b = get_random_float( -FLT_MAX, FLT_MAX, d );
                 }
 
-                ii = i << 1;
                 if ((flag_set[src_flag_id] & CL_MEM_USE_HOST_PTR) || (flag_set[src_flag_id] & CL_MEM_COPY_HOST_PTR))
-                    buffers[ii] = clCreateBuffer(context, flag_set[src_flag_id],  ptrSizes[i] * num_elements, inptr[i], &err);
+                    buffers[0] = clCreateBuffer(context, flag_set[src_flag_id],
+                                                ptrSizes[i] * num_elements,
+                                                inptr[i], &err);
                 else
-                    buffers[ii] = clCreateBuffer(context, flag_set[src_flag_id],  ptrSizes[i] * num_elements, NULL, &err);
+                    buffers[0] =
+                        clCreateBuffer(context, flag_set[src_flag_id],
+                                       ptrSizes[i] * num_elements, NULL, &err);
                 if ( err ){
                     align_free( outptr[i] );
                     print_error(err, " clCreateBuffer failed\n" );
@@ -839,10 +860,15 @@
                 }
                 outptr[i] = align_malloc( ptrSizes[i] * num_elements, min_alignment);
                 if ((flag_set[dst_flag_id] & CL_MEM_USE_HOST_PTR) || (flag_set[dst_flag_id] & CL_MEM_COPY_HOST_PTR))
-                    buffers[ii+1] = clCreateBuffer(context, flag_set[dst_flag_id],  ptrSizes[i] * num_elements, outptr[i], &err);
+                    buffers[1] = clCreateBuffer(context, flag_set[dst_flag_id],
+                                                ptrSizes[i] * num_elements,
+                                                outptr[i], &err);
                 else
-                    buffers[ii+1] = clCreateBuffer(context, flag_set[dst_flag_id],  ptrSizes[i] * num_elements, NULL, &err);
-                if ( ! buffers[ii+1] || err){
+                    buffers[1] =
+                        clCreateBuffer(context, flag_set[dst_flag_id],
+                                       ptrSizes[i] * num_elements, NULL, &err);
+                if (!buffers[1] || err)
+                {
                     align_free( outptr[i] );
                     print_error(err, " clCreateBuffer failed\n" );
                     free_mtdata(d);
@@ -851,7 +877,9 @@
 
                 if (gTestMap) {
                     void *dataPtr;
-                    dataPtr = clEnqueueMapBuffer(queue, buffers[ii], CL_TRUE, CL_MAP_WRITE, 0, ptrSizes[i]*num_elements, 0, NULL, NULL, &err);
+                    dataPtr = clEnqueueMapBuffer(
+                        queue, buffers[0], CL_TRUE, CL_MAP_WRITE, 0,
+                        ptrSizes[i] * num_elements, 0, NULL, NULL, &err);
                     if (err) {
                         print_error(err, "clEnqueueMapBuffer failed");
                         align_free( outptr[i] );
@@ -861,7 +889,8 @@
 
                     memcpy(dataPtr, inptr[i], ptrSizes[i]*num_elements);
 
-                    err = clEnqueueUnmapMemObject(queue, buffers[ii], dataPtr, 0, NULL, NULL);
+                    err = clEnqueueUnmapMemObject(queue, buffers[0], dataPtr, 0,
+                                                  NULL, NULL);
                     if (err) {
                         print_error(err, "clEnqueueUnmapMemObject failed");
                         align_free( outptr[i] );
@@ -870,7 +899,9 @@
                     }
                 }
                 else if (!(flag_set[src_flag_id] & CL_MEM_USE_HOST_PTR) && !(flag_set[src_flag_id] & CL_MEM_COPY_HOST_PTR)) {
-                    err = clEnqueueWriteBuffer(queue, buffers[ii], CL_TRUE, 0, ptrSizes[i]*num_elements, inptr[i], 0, NULL, NULL);
+                    err = clEnqueueWriteBuffer(queue, buffers[0], CL_TRUE, 0,
+                                               ptrSizes[i] * num_elements,
+                                               inptr[i], 0, NULL, NULL);
                     if ( err != CL_SUCCESS ){
                         align_free( outptr[i] );
                         print_error( err, " clWriteBuffer failed" );
@@ -879,8 +910,10 @@
                     }
                 }
 
-                err = clSetKernelArg( kernel[i], 0, sizeof( cl_mem ), (void *)&buffers[ii] );
-                err |= clSetKernelArg( kernel[i], 1, sizeof( cl_mem ), (void *)&buffers[ii+1] );
+                err = clSetKernelArg(kernel[i], 0, sizeof(cl_mem),
+                                     (void *)&buffers[0]);
+                err |= clSetKernelArg(kernel[i], 1, sizeof(cl_mem),
+                                      (void *)&buffers[1]);
                 if ( err != CL_SUCCESS ){
                     align_free( outptr[i] );
                     print_error( err, " clSetKernelArg failed" );
@@ -896,7 +929,9 @@
                     return -1;
                 }
 
-                err = clEnqueueReadBuffer( queue, buffers[ii+1], true, 0, ptrSizes[i]*num_elements, outptr[i], 0, NULL, NULL );
+                err = clEnqueueReadBuffer(queue, buffers[1], true, 0,
+                                          ptrSizes[i] * num_elements, outptr[i],
+                                          0, NULL, NULL);
                 if ( err != CL_SUCCESS ){
                     align_free( outptr[i] );
                     print_error( err, " clEnqueueReadBuffer failed" );
diff --git a/test_conformance/compiler/test_compiler_defines_for_extensions.cpp b/test_conformance/compiler/test_compiler_defines_for_extensions.cpp
index a4a8251..483adac 100644
--- a/test_conformance/compiler/test_compiler_defines_for_extensions.cpp
+++ b/test_conformance/compiler/test_compiler_defines_for_extensions.cpp
@@ -335,10 +335,10 @@
     strcat(kernel_code, kernel_strings[4]);
 
     // Now we need to execute the kernel
-    cl_mem defines;
+    clMemWrapper defines;
     cl_int *data;
-    cl_program program;
-    cl_kernel kernel;
+    clProgramWrapper program;
+    clKernelWrapper kernel;
 
     Version version = get_device_cl_version(device);
 
@@ -432,10 +432,6 @@
       free(extensions_supported[i]);
     }
     free(extensions);
-    if( defines ) {
-        error = clReleaseMemObject( defines );
-        test_error( error, "Unable to release memory object" );
-    }
 
     if (total_errors)
         return -1;