maierfelix / webgpu

WebGPU for Node [Deprecated, Unmaintained]
MIT License
244 stars 17 forks source link

Resource leak in GPUCanvasContext::getSwapChainPreferredFormat #35

Open sparist opened 2 years ago

sparist commented 2 years ago

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();
  }
sparist commented 2 years ago

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.