Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unityFramework?.pause(true) throw null pointer exceptions #597

Open
FrenziedAI opened this issue Jun 26, 2024 · 3 comments
Open

unityFramework?.pause(true) throw null pointer exceptions #597

FrenziedAI opened this issue Jun 26, 2024 · 3 comments

Comments

@FrenziedAI
Copy link

When calling unityFramework?.pause(true) to pause Unity from iOS and then calling unityFramework?.pause(false) to resume it, if there are UniTask code snippets that are currently executing, they may throw null pointer exceptions after resuming, while not pausing Unity does not cause this issue.

@FrenziedAI
Copy link
Author

This code snippet

Debug.Log("LoadThuman " + bornPoint.ToString() + Button_Test.ToString() + this.ToString() + obj.ToString() + newAvatarData.ToString() + Instance.ToString() + UIMgr.Instance.ToString() + CommonData.m_Player.ToString());

Where:
bornPoint and Button_Test are GameObject references of this on the Inspector,
obj is retrieved via GameObject.Find("bornPoint");,
newAvatarData is an instance that is created before the await statement in a task,
Instance refers to a static object of this,
UIMgr.Instance and CommonData.m_Player are static instances of other objects.

Under normal conditions (when iOS is not paused), the output is
LoadThumanbornPoint (UnityEngine.Transform)Button_Test (UnityEngine.UI.Button)UIWardrobe (Tsl.Ymkj.Partner.UIWardrobe)bornPoint (UnityEngine.GameObject)THumanAvatarVoUIWardrobe
.

However, under circumstances when iOS is paused, there is a probability that the output will be
LoadThumannullnullnullbornPoint (UnityEngine.GameObject)THumanAvatarVoUIWardrobe (Tsl.Ymkj.Partner.UIWardrobe)UIMgr (Tsl.Ymkj.Partner.UIMgr).

I have not yet pinpointed the exact moment where this gets lost.

@FrenziedAI
Copy link
Author

image

UniTask Test4 UIWardrobe (Tsl.Ymkj.Partner.UIWardrobe)UniTask Test4 UIWardrobe (Tsl.Ymkj.Partner.UIWardrobe)
UniTask Test5 null

UniTask Test4 UIWardrobe (Tsl.Ymkj.Partner.UIWardrobe) Tsl.Ymkj.Partner.THumanManager:DownLoadAssetBundle(String, String, UIWardrobe) Tsl.Ymkj.Partner.<LoadAsync>d__54:MoveNext() Cysharp.Threading.Tasks.CompilerServices.AsyncUniTaskMethodBuilder1:Start(d__54&)
Tsl.Ymkj.Partner.THumanManager:LoadAsync(UIWardrobe, THumanAvatarVo, Transform, String, Boolean, Boolean)
Tsl.Ymkj.Partner.d__40:MoveNext()
Cysharp.Threading.Tasks.CompilerServices.AsyncUniTaskMethodBuilder:Start(d__40&)
Tsl.Ymkj.Partner.UIWardrobe:LoadThuman(String)
Tsl.Ymkj.Partner.d__3:MoveNext()
Cysharp.Threading.Tasks.CompilerServices.AsyncUniTask1:SetResult() Tsl.Ymkj.Partner.<UpdateWardrobeDataAll>d__7:MoveNext() Cysharp.Threading.Tasks.CompilerServices.AsyncUniTask1:Run()
Cysharp.Threading.Tasks.CompilerServices.AsyncUniTask1:SetResult() Tsl.Ymkj.Partner.<UpdateWardrobeData>d__10:MoveNext() Cysharp.Threading.Tasks.CompilerServices.AsyncUniTask1:Run()
System.Diagnostics.StackTrace:init_frames(Int32, Boolean)
UnityEngine.StackTraceUtility:ExtractStackTrace()
System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback, Object, Boolean)
System.Runtime.CompilerServices.MoveNextRunner:Run()
Cysharp.Threading.Tasks.UniTaskCompletionSourceCore1:TrySetResult(TResult) Cysharp.Threading.Tasks.UniTaskCompletionSourceCore1:TrySetResult(TResult)
Cysharp.Threading.Tasks.UniTaskCompletionSourceCore1:TrySetResult(TResult) <Post>d__0:MoveNext() Cysharp.Threading.Tasks.UniTaskCompletionSourceCore1:TrySetResult(TResult)
d__7:MoveNext()
Cysharp.Threading.Tasks.UniTaskCompletionSource`1:TrySignalCompletion(UniTaskStatus)
System.Threading.Tasks.Task:Execute()
System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback, Object, Boolean)
System.Threading.Tasks.Task:ExecuteWithThreadLocal(Task&)
System.Threading.Tasks.Task:ExecuteEntry(Boolean)
UnityEngine.WorkRequest:Invoke()
UnityEngine.UnitySynchronizationContext:Exec()

The referenced script on this Behaviour (Game Object 'Main Camera') is missing!
Unloading 3 Unused Serialized files (Serialized files now loaded: 8)
UnloadTime: 3.634542 ms
[UnityToNativeJson -> {"methodName":"EnterMainScense","content":""}
UnityEngine.StackTraceUtility:ExtractStackTrace()
UnityToNative:UnityToNativeInvokeMethod(String, String)

2024-06-27 14:03:27 [unity debug] : 收到U3D的消息: {"methodName":"EnterMainScense","content":""}
GetUserDigitalPersonInfo semaphoreSlim WaitAsync
Tsl.Ymkj.Partner.GetMetahumanInfo:GetUserDigitalPersonInfo()
Tsl.Ymkj.Partner.GetMetahumanInfo:Start()
System.Diagnostics.StackTrace:init_frames(Int32, Boolean)
UnityEngine.StackTraceUtility:ExtractStackTrace()

Unloading 2 unused Assets to reduce memory usage. Loaded Objects now: 7630.
Total: 5.003959 ms (FindLiveObjects: 0.423625 ms CreateObjectMapping: 0.207208 ms MarkObjects: 4.339917 ms DeleteObjects: 0.033167 ms)

UniTask Test5 null
`

@FrenziedAI
Copy link
Author

After the line
await unityWebRequest.SendWebRequest();
the references within the task (TSK) seem to be lost.

MarquisMc added a commit to MarquisMc/UniTask that referenced this issue Oct 22, 2024
Fixes Cysharp#597

Add null reference checks and reinitialize references after resuming Unity to prevent null pointer exceptions in UniTask code snippets.

* **AsyncUniTaskMethodBuilder.cs**
  - Add null reference checks in `SetResult`, `SetException`, `AwaitOnCompleted`, and `AwaitUnsafeOnCompleted` methods.
* **PlayerLoopRunner.cs**
  - Add code to reinitialize references after resuming in `RunCore` method.
* **PlayerLoopHelper.cs**
  - Add code to ensure references are not lost after resuming in `AddAction` and `AddContinuation` methods.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/Cysharp/UniTask/issues/597?shareId=XXXX-XXXX-XXXX-XXXX).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant