Open katboi01 opened 5 days ago
Hi, thank you for your detailed report and for providing a reproduction sample. From your description, it seems the problem occurs due to shared memory (_dataArray in the .jslib) being modified or deallocated unexpectedly. The symptoms, such as VR session freezing or the Enter VR button becoming unresponsive, suggest a potential conflict between UnityWebRequest, the underlying WebGL memory model, and how SimpleWebXR manages its shared arrays.
Garbage Collection (GC): As you mentioned, the shared memory could be moved or collected by Unity's GC or the browser’s WebAssembly memory model. If _dataArray
is not properly pinned or its lifecycle isn't tightly controlled, operations like downloading assets or parsing JSON may inadvertently trigger issues.
Thread Blocking: While UnityWebRequest operates asynchronously, it might still interfere with the WebGL main thread in specific cases, especially if Unity or the browser tries to allocate/deallocate memory during runtime.
Order of Initialization: Moving SimpleWebXR.EnsureInstance();
after the UnityWebRequest
resolves the issue, which suggests that the timing of initialization and resource allocation is critical.
While we investigate further, here are some possible ways to mitigate the issue:
Ensure Proper Memory Handling:
_dataArray
in the .jslib is initialized and used consistently. If possible, manually pin it or ensure it's re-initialized before critical VR operations._dataArray
during file downloads unless necessary.Avoid Simultaneous Memory Operations:
Test on Different Browsers:
Hello, when trying to implement the library, I've come across an issue. My application relies heavily on downloading assets at runtime. I noticed, that when downloading and parsing files, the Enter VR button would sometimes stop working, or the VR view would freeze if a session was already running. By adding console.log() in various parts of the .jslib, I found that
_dataArray
would become unassigned/set to all zeroes after downloading a file. I think it may be related to garbage collection, or some other memory optimization. I tried using malloc in the .jslib script, and GCHandle in pinned mode in the .cs to ensure that memory does not get moved arround, however, I was unable to work around this issue. I tested the issue in Unity versions 2020.3.48, 2021.3.31 and 2022.3.37. I've come up with a minimal reproduction sample: Scene:Script.cs
If
SimpleWebXR.EnsureInstance();
is moved behind theusing
statement, the issue does not occur, however in my case, files can be downloaded at any time, which causes the shared array to desync.Url comes from a public repository https://github.com/SimpleSandman/UmaMusumeAPI
There are no errors in the console or anything the like when the problem occurs. I'd be grateful if someone with more knowledge about the implementation could look into this.