swapchain: Fixed potential memory leaks in exceptional situations. 47/178947/1 accepted/tizen/unified/20180518.120500 submit/tizen/20180516.003940
authorjoonbum.ko <joonbum.ko@samsung.com>
Tue, 15 May 2018 04:37:36 +0000 (13:37 +0900)
committerjoonbum.ko <joonbum.ko@samsung.com>
Tue, 15 May 2018 04:37:36 +0000 (13:37 +0900)
Change-Id: I4adf153e43382cfc74dac94b386b4248067d3b6e
Signed-off-by: joonbum.ko <joonbum.ko@samsung.com>
src/wsi/swapchain.c

index 18aef02..d779d41 100644 (file)
@@ -97,6 +97,7 @@ vk_CreateSwapchainKHR(VkDevice                                                         device,
        vk_swapchain_t          *chain;
        VkResult                         error;
        uint32_t                         i;
+       uint32_t                         cnt_success = 0;
        tbm_surface_h           *buffers;
        tbm_format                       format;
        vk_icd_t                        *icd = vk_get_icd();
@@ -126,6 +127,7 @@ vk_CreateSwapchainKHR(VkDevice                                                       device,
 
        chain->allocator = allocator;
        chain->surface = info->surface;
+       chain->buffers = NULL;
 
        format = get_tbm_format(info->imageFormat, info->compositeAlpha);
        VK_CHECK(format, return VK_ERROR_SURFACE_LOST_KHR, "Not supported image format.\n");
@@ -160,8 +162,11 @@ vk_CreateSwapchainKHR(VkDevice                                                      device,
                };
 
                chain->buffers[i].tbm = buffers[i];
-               icd->create_presentable_image(device, chain->buffers[i].tbm, &image_info,
-                                                                         allocator, &chain->buffers[i].image);
+               error = icd->create_presentable_image(device, chain->buffers[i].tbm, &image_info,
+                                                                                         allocator, &chain->buffers[i].image);
+               VK_CHECK(error == VK_SUCCESS, goto done, "create_presentable_image failed.\n");
+
+               cnt_success++;
        }
        goto done;
 
@@ -170,11 +175,29 @@ error_mem_alloc:
 
 done:
        if (error != VK_SUCCESS) {
+               if (cnt_success > 0) {
+                       PFN_vkGetDeviceProcAddr  icd_gdpa = (PFN_vkGetDeviceProcAddr)icd->get_proc_addr(NULL, "vkGetDeviceProcAddr");
+                       if (icd_gdpa != VK_NULL_HANDLE) {
+                               PFN_vkDestroyImage               destroy_image = (PFN_vkDestroyImage)icd_gdpa(device, "vkDestroyImage");
+                               if (destroy_image != VK_NULL_HANDLE) {
+                                       for (i = 0; i < cnt_success; i++)
+                                               destroy_image(device, chain->buffers[i].image, allocator);
+                               }
+                       }
+               }
+
+               if (chain->buffers) {
+                       vk_free(allocator, chain->buffers);
+                       chain->buffers = NULL;
+               }
+
                if (chain->deinit)
                        chain->deinit(device, chain);
 
-               if (chain)
+               if (chain) {
                        vk_free(allocator, chain);
+                       chain = NULL;
+               }
 
                *swapchain = VK_NULL_HANDLE;
        } else {