Skip to content

Commit bb01b11

Browse files
author
benstevens48
committed
fix device context pool deadlock scenario
1 parent 9a16008 commit bb01b11

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

winrt/lib/drawing/DeviceContextPool.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,27 @@ DeviceContextPool::DeviceContextPool(ID2D1Device1* d2dDevice)
2020

2121
DeviceContextLease DeviceContextPool::TakeLease()
2222
{
23-
Lock lock(m_mutex);
24-
25-
if (!m_d2dDevice)
26-
ThrowHR(RO_E_CLOSED);
27-
28-
if (m_deviceContexts.empty())
23+
ID2D1Device1* localDevice;
2924
{
30-
ComPtr<ID2D1DeviceContext1> deviceContext;
31-
ThrowIfFailed(m_d2dDevice->CreateDeviceContext(
32-
D2D1_DEVICE_CONTEXT_OPTIONS_NONE,
33-
&deviceContext));
34-
return DeviceContextLease(this, std::move(deviceContext));
35-
}
36-
else
37-
{
38-
DeviceContextLease newLease(this, std::move(m_deviceContexts.back()));
39-
m_deviceContexts.pop_back();
40-
return newLease;
25+
Lock lock(m_mutex);
26+
27+
if (!m_deviceContexts.empty())
28+
{
29+
DeviceContextLease newLease(this, std::move(m_deviceContexts.back()));
30+
m_deviceContexts.pop_back();
31+
return newLease;
32+
}
33+
localDevice = m_d2dDevice;
4134
}
35+
//Release lock before creating the device context to avoid potential deadlock scenarios
36+
if (!localDevice)
37+
ThrowHR(RO_E_CLOSED);
38+
39+
ComPtr<ID2D1DeviceContext1> deviceContext;
40+
ThrowIfFailed(localDevice->CreateDeviceContext(
41+
D2D1_DEVICE_CONTEXT_OPTIONS_NONE,
42+
&deviceContext));
43+
return DeviceContextLease(this, std::move(deviceContext));
4244
}
4345

4446

0 commit comments

Comments
 (0)