[c-host] dxgi: fixed memory leak

This commit is contained in:
Geoffrey McRae 2019-03-04 12:04:17 +11:00
parent 79ce98116a
commit f79a1b2533

View file

@ -128,14 +128,11 @@ static bool dxgi_init()
for(int n = 0; IDXGIAdapter1_EnumOutputs(adapter, n, &output) != DXGI_ERROR_NOT_FOUND; ++n) for(int n = 0; IDXGIAdapter1_EnumOutputs(adapter, n, &output) != DXGI_ERROR_NOT_FOUND; ++n)
{ {
IDXGIOutput_GetDesc(output, &outputDesc); IDXGIOutput_GetDesc(output, &outputDesc);
if (!outputDesc.AttachedToDesktop) if (outputDesc.AttachedToDesktop)
{ break;
IDXGIOutput_Release(output);
output = NULL;
continue;
}
break; IDXGIOutput_Release(output);
output = NULL;
} }
if (output) if (output)
@ -148,7 +145,7 @@ static bool dxgi_init()
if (!output) if (!output)
{ {
DEBUG_ERROR("Failed to locate a valid output device"); DEBUG_ERROR("Failed to locate a valid output device");
goto fail; goto fail_release_factory;
} }
static const D3D_FEATURE_LEVEL featureLevels[] = static const D3D_FEATURE_LEVEL featureLevels[] =
@ -196,13 +193,13 @@ static bool dxgi_init()
this->width = outputDesc.DesktopCoordinates.right - outputDesc.DesktopCoordinates.left; this->width = outputDesc.DesktopCoordinates.right - outputDesc.DesktopCoordinates.left;
this->height = outputDesc.DesktopCoordinates.bottom - outputDesc.DesktopCoordinates.top; this->height = outputDesc.DesktopCoordinates.bottom - outputDesc.DesktopCoordinates.top;
DEBUG_INFO("Device Descripion: %ls" , adapterDesc.Description); DEBUG_INFO("Device Descripion: %ls" , adapterDesc.Description);
DEBUG_INFO("Device Vendor ID : 0x%x" , adapterDesc.VendorId); DEBUG_INFO("Device Vendor ID : 0x%x" , adapterDesc.VendorId);
DEBUG_INFO("Device Device ID : 0x%x" , adapterDesc.DeviceId); DEBUG_INFO("Device Device ID : 0x%x" , adapterDesc.DeviceId);
DEBUG_INFO("Device Video Mem : %u MiB", (unsigned)(adapterDesc.DedicatedVideoMemory / 1048576)); DEBUG_INFO("Device Video Mem : %u MiB" , (unsigned)(adapterDesc.DedicatedVideoMemory / 1048576));
DEBUG_INFO("Device Sys Mem : %u MiB", (unsigned)(adapterDesc.DedicatedSystemMemory / 1048576)); DEBUG_INFO("Device Sys Mem : %u MiB" , (unsigned)(adapterDesc.DedicatedSystemMemory / 1048576));
DEBUG_INFO("Shared Sys Mem : %u MiB", (unsigned)(adapterDesc.SharedSystemMemory / 1048576)); DEBUG_INFO("Shared Sys Mem : %u MiB" , (unsigned)(adapterDesc.SharedSystemMemory / 1048576));
DEBUG_INFO("Feature Level : 0x%x", this->featureLevel); DEBUG_INFO("Feature Level : 0x%x" , this->featureLevel);
DEBUG_INFO("Capture Size : %u x %u", this->width, this->height); DEBUG_INFO("Capture Size : %u x %u", this->width, this->height);
// bump up our priority // bump up our priority
@ -249,6 +246,7 @@ static bool dxgi_init()
IDXGIOutput1_Release(output1); IDXGIOutput1_Release(output1);
goto fail_release_device; goto fail_release_device;
} }
IDXGIOutput1_Release(output1);
} }
else else
{ {
@ -286,6 +284,7 @@ static bool dxgi_init()
IDXGIOutput5_Release(output5); IDXGIOutput5_Release(output5);
goto fail_release_device; goto fail_release_device;
} }
IDXGIOutput5_Release(output5);
} }
DXGI_OUTDUPL_DESC dupDesc; DXGI_OUTDUPL_DESC dupDesc;
@ -329,7 +328,7 @@ static bool dxgi_init()
if (FAILED(status)) if (FAILED(status))
{ {
DEBUG_WINERROR("Failed to map the texture", status); DEBUG_WINERROR("Failed to map the texture", status);
goto fail_release_output; goto fail_release_texture;
} }
this->pitch = mapping.RowPitch; this->pitch = mapping.RowPitch;
this->stride = mapping.RowPitch / 4; this->stride = mapping.RowPitch / 4;
@ -341,15 +340,21 @@ static bool dxgi_init()
this->initialized = true; this->initialized = true;
return true; return true;
fail_release_texture:
ID3D11Texture2D_Release(this->texture);
this->texture = NULL;
fail_release_output: fail_release_output:
IDXGIOutputDuplication_Release(this->dup); IDXGIOutputDuplication_Release(this->dup);
this->dup = NULL; this->dup = NULL;
fail_release_device: fail_release_device:
ID3D11Device_Release(this->device); ID3D11Device_Release (this->device );
this->device = NULL; ID3D11DeviceContext_Release(this->deviceContext);
this->device = NULL;
this->deviceContext = NULL;
fail_release: fail_release:
IDXGIOutput_Release (output ); IDXGIOutput_Release (output );
IDXGIAdapter1_Release(adapter); IDXGIAdapter1_Release(adapter);
fail_release_factory:
IDXGIFactory1_Release(factory); IDXGIFactory1_Release(factory);
fail: fail:
return false; return false;
@ -359,8 +364,6 @@ static bool dxgi_deinit()
{ {
assert(this); assert(this);
os_signalEvent(this->copyEvent);
if (this->texture) if (this->texture)
{ {
ID3D11Texture2D_Release(this->texture); ID3D11Texture2D_Release(this->texture);
@ -374,10 +377,10 @@ static bool dxgi_deinit()
this->dup = NULL; this->dup = NULL;
} }
if (this->device) if (this->deviceContext)
{ {
ID3D11Device_Release(this->device); ID3D11DeviceContext_Release(this->deviceContext);
this->device = NULL; this->deviceContext = NULL;
} }
if (this->output) if (this->output)
@ -386,6 +389,12 @@ static bool dxgi_deinit()
this->output = NULL; this->output = NULL;
} }
if (this->device)
{
ID3D11Device_Release(this->device);
this->device = NULL;
}
if (this->adapter) if (this->adapter)
{ {
IDXGIAdapter1_Release(this->adapter); IDXGIAdapter1_Release(this->adapter);
@ -394,8 +403,14 @@ static bool dxgi_deinit()
if (this->factory) if (this->factory)
{ {
IDXGIFactory1_Release(this->factory); // if this doesn't free we have a memory leak
DWORD count = IDXGIFactory1_Release(this->factory);
this->factory = NULL; this->factory = NULL;
if (count != 0)
{
DEBUG_ERROR("Factory release is %lu, there is a memory leak!", count);
return false;
}
} }
this->initialized = false; this->initialized = false;
@ -456,17 +471,17 @@ static CaptureResult dxgi_capture(bool * hasFrameUpdate, bool * hasPointerUpdate
return CAPTURE_RESULT_ERROR; return CAPTURE_RESULT_ERROR;
} }
ID3D11Texture2D * src;
status = IDXGIResource_QueryInterface(res, &IID_ID3D11Texture2D, (void **)&src);
if (FAILED(status))
{
DEBUG_WINERROR("Failed to get the texture from the dxgi resource", status);
IDXGIResource_Release(res);
return CAPTURE_RESULT_ERROR;
}
if (frameInfo.LastPresentTime.QuadPart != 0) if (frameInfo.LastPresentTime.QuadPart != 0)
{ {
ID3D11Texture2D * src;
status = IDXGIResource_QueryInterface(res, &IID_ID3D11Texture2D, (void **)&src);
if (FAILED(status))
{
DEBUG_WINERROR("Failed to get the texture from the dxgi resource", status);
IDXGIResource_Release(res);
return CAPTURE_RESULT_ERROR;
}
// wait for the prior copy to finish in getFrame // wait for the prior copy to finish in getFrame
os_waitEvent(this->copyEvent); os_waitEvent(this->copyEvent);