Fix possible OpenXR swapchain leak by using new unique_oxr_ptr
authorJulian Eisel <eiseljulian@gmail.com>
Wed, 17 Jul 2019 12:02:59 +0000 (14:02 +0200)
committerJulian Eisel <eiseljulian@gmail.com>
Wed, 17 Jul 2019 12:05:06 +0000 (14:05 +0200)
intern/ghost/intern/GHOST_XrSession.cpp

index 986e119ea9013c2bed9283fee11190af3eb70742..2e85c22faad3b600686310a6389a13b9f399eed0 100644 (file)
@@ -234,12 +234,12 @@ static std::vector<XrSwapchainImageBaseHeader *> swapchain_images_create(
   return images;
 }
 
-static XrSwapchain swapchain_create(const XrSession session,
-                                    GHOST_IXrGraphicsBinding *gpu_binding,
-                                    const XrViewConfigurationView *xr_view)
+static unique_oxr_ptr<XrSwapchain> swapchain_create(const XrSession session,
+                                                    GHOST_IXrGraphicsBinding *gpu_binding,
+                                                    const XrViewConfigurationView *xr_view)
 {
   XrSwapchainCreateInfo create_info{XR_TYPE_SWAPCHAIN_CREATE_INFO};
-  XrSwapchain swapchain;
+  unique_oxr_ptr<XrSwapchain> swapchain(xrDestroySwapchain);
   uint32_t format_count = 0;
   int64_t chosen_format;
 
@@ -264,7 +264,7 @@ static XrSwapchain swapchain_create(const XrSession session,
   create_info.faceCount = 1;
   create_info.arraySize = 1;
   create_info.mipCount = 1;
-  CHECK_XR(xrCreateSwapchain(session, &create_info, &swapchain),
+  CHECK_XR(swapchain.construct(xrCreateSwapchain, session, &create_info),
            "Failed to create OpenXR swapchain.");
 
   return swapchain;
@@ -289,13 +289,16 @@ void GHOST_XrSession::prepareDrawing()
            "Failed to get count of view configurations.");
 
   for (const XrViewConfigurationView &view : view_configs) {
-    XrSwapchain swapchain = swapchain_create(m_oxr->session, m_gpu_binding.get(), &view);
-    auto images = swapchain_images_create(swapchain, m_gpu_binding.get());
+    unique_oxr_ptr<XrSwapchain> swapchain = swapchain_create(
+        m_oxr->session, m_gpu_binding.get(), &view);
+    auto images = swapchain_images_create(swapchain.get(), m_gpu_binding.get());
 
     m_oxr->swapchain_image_width = view.recommendedImageRectWidth;
     m_oxr->swapchain_image_height = view.recommendedImageRectHeight;
-    m_oxr->swapchains.push_back(swapchain);
-    m_oxr->swapchain_images.insert(std::make_pair(swapchain, std::move(images)));
+    m_oxr->swapchains.push_back(swapchain.get());
+    m_oxr->swapchain_images.insert(std::make_pair(swapchain.get(), std::move(images)));
+
+    swapchain.release();
   }
 
   m_oxr->views.resize(view_count, {XR_TYPE_VIEW});