From ee12dd9ae7dc012092bd27f78e15e6dbb101074e Mon Sep 17 00:00:00 2001 From: hadashiA Date: Thu, 25 Jan 2024 17:47:06 +0900 Subject: [PATCH] Fix omissions in Addressable.Release and IsValid checks --- .../AddressablesAsyncExtensions.cs | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs index 0f3a25c..55de21e 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs @@ -109,7 +109,7 @@ namespace Cysharp.Threading.Tasks TaskPool.RegisterSizeGetter(typeof(AsyncOperationHandleConfiguredSource), () => pool.Size); } - readonly Action continuationAction; + readonly Action completedCallback; AsyncOperationHandle handle; CancellationToken cancellationToken; CancellationTokenRegistration cancellationTokenRegistration; @@ -120,7 +120,7 @@ namespace Cysharp.Threading.Tasks AsyncOperationHandleConfiguredSource() { - continuationAction = Continuation; + completedCallback = HandleCompleted; } public static IUniTaskSource Create(AsyncOperationHandle handle, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) @@ -145,6 +145,10 @@ namespace Cysharp.Threading.Tasks result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => { var promise = (AsyncOperationHandleConfiguredSource)state; + if (promise.handle.IsValid()) + { + Addressables.Release(promise.handle); + } promise.core.TrySetCanceled(promise.cancellationToken); }, result); } @@ -153,15 +157,18 @@ namespace Cysharp.Threading.Tasks PlayerLoopHelper.AddAction(timing, result); - handle.Completed += result.continuationAction; + handle.Completed += result.completedCallback; token = result.core.Version; return result; } - void Continuation(AsyncOperationHandle _) + void HandleCompleted(AsyncOperationHandle _) { - handle.Completed -= continuationAction; + if (handle.IsValid()) + { + handle.Completed -= completedCallback; + } if (completed) { @@ -172,7 +179,7 @@ namespace Cysharp.Threading.Tasks completed = true; if (cancellationToken.IsCancellationRequested) { - If (handle.IsValid()) + if (handle.IsValid()) { Addressables.Release(handle); } @@ -299,7 +306,7 @@ namespace Cysharp.Threading.Tasks TaskPool.RegisterSizeGetter(typeof(AsyncOperationHandleConfiguredSource), () => pool.Size); } - readonly Action> continuationAction; + readonly Action> completedCallback; AsyncOperationHandle handle; CancellationToken cancellationToken; CancellationTokenRegistration cancellationTokenRegistration; @@ -310,7 +317,7 @@ namespace Cysharp.Threading.Tasks AsyncOperationHandleConfiguredSource() { - continuationAction = Continuation; + completedCallback = HandleCompleted; } public static IUniTaskSource Create(AsyncOperationHandle handle, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) @@ -335,6 +342,10 @@ namespace Cysharp.Threading.Tasks result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => { var promise = (AsyncOperationHandleConfiguredSource)state; + if (promise.handle.IsValid()) + { + Addressables.Release(promise.handle); + } promise.core.TrySetCanceled(promise.cancellationToken); }, result); } @@ -343,15 +354,18 @@ namespace Cysharp.Threading.Tasks PlayerLoopHelper.AddAction(timing, result); - handle.Completed += result.continuationAction; + handle.Completed += result.completedCallback; token = result.core.Version; return result; } - void Continuation(AsyncOperationHandle argHandle) + void HandleCompleted(AsyncOperationHandle argHandle) { - handle.Completed -= continuationAction; + if (handle.IsValid()) + { + handle.Completed -= completedCallback; + } if (completed) {