maierfelix/webgpu

Resource leak in GPUCanvasContext::getSwapChainPreferredFormat

Opened this issue · 1 comments

It appears that the new WGPUSwapChainImpl pointer returned by wgpuDeviceCreateSwapChain() does not get deleted; this causes AddressSanitizer to flag the leak. See the instance variable in the code snippet below:

  if (window->preferredSwapChainFormat == WGPUTextureFormat_Undefined) {
    WGPUSwapChainDescriptor descriptor;
    descriptor.nextInChain = nullptr;
    // returns always the same address, so we dont have to release this temp swapchain?
    descriptor.implementation = device->binding->GetSwapChainImplementation();
    WGPUSwapChain instance = wgpuDeviceCreateSwapChain(device->instance, nullptr, &descriptor);
    glfwPollEvents();
    window->preferredSwapChainFormat = device->binding->GetPreferredSwapChainTextureFormat();
  }

The tricky thing with fixing this is that WGPUSwapChain is a typedef of struct WGPUSwapChainImpl *, which is an incomplete type at this point, so calling delete on it is ill-advised. In order to delete it properly, the code needs to have access to the definition of struct WGPUSwapChainImpl, but I can't seem to find it.