Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 668b87c

Browse files
Call CancellationTokenRegistration.Unregister in AsyncInfoToTaskBridge to avoid leaking memory when the CancellationTokenSource is reused. (#28471)
Addresses DevDiv 403992.
1 parent c07015e commit 668b87c

File tree

2 files changed

+4
-15
lines changed

2 files changed

+4
-15
lines changed

src/System.Runtime.WindowsRuntime/src/System/InternalHelpers.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,15 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Threading.Tasks;
6-
using System.Threading;
5+
using System.Runtime.InteropServices;
76

87
namespace System
98
{
109
internal static class InternalHelpers
1110
{
1211
internal static void SetErrorCode(this Exception ex, int code)
1312
{
14-
// Stub, until COM interop guys fix the exception logic
15-
}
16-
17-
internal static void TryDeregister(this CancellationTokenRegistration ctr)
18-
{
19-
//nothing to do for projectN
13+
InteropExtensions.SetExceptionErrorCode(ex, code);
2014
}
2115
}
2216
}

src/System.Runtime.WindowsRuntime/src/System/Threading/Tasks/AsyncInfoToTaskBridge.CoreRT.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
65
using Internal.Interop;
76
using Internal.Threading.Tasks;
8-
using System.ComponentModel;
97
using System.Diagnostics;
108
using System.Diagnostics.Contracts;
11-
using System.Runtime.CompilerServices;
12-
using System.Runtime.InteropServices.WindowsRuntime;
139
using System.Runtime.InteropServices;
14-
using System.Threading;
1510
using Windows.Foundation;
1611

1712
namespace System.Threading.Tasks
@@ -68,7 +63,7 @@ internal void RegisterForCancellation(IAsyncInfo asyncInfo)
6863
}
6964

7065
if (disposeOfCtr)
71-
ctr.TryDeregister();
66+
ctr.Unregister();
7267
}
7368
}
7469
catch (Exception ex)
@@ -151,7 +146,7 @@ private void Complete(IAsyncInfo asyncInfo, Func<IAsyncInfo, TResult> getResults
151146
ctr = _ctr; // under lock to avoid torn reads
152147
_ctr = default(CancellationTokenRegistration);
153148
}
154-
ctr.TryDeregister(); // It's ok if we end up unregistering a not-initialized registration; it'll just be a nop.
149+
ctr.Unregister(); // It's ok if we end up unregistering a not-initialized registration; it'll just be a nop.
155150

156151
try
157152
{

0 commit comments

Comments
 (0)