Skip to content

Shared array breaks after performing memory intensive operations #60

@katboi01

Description

@katboi01

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:

  • single camera with the Script.cs script attached.

Script.cs

using System.Collections.Generic;
using UnityEngine.Networking;
using Newtonsoft.Json.Linq;
using Rufus31415.WebXR;
using System.Collections;
using System.Linq;
using UnityEngine;

public class Script : MonoBehaviour
{
    public string Url = "https://www.tracenacademy.com/api/MobData";
    public List<string> Characters;

    private IEnumerator Start()
    {
        SimpleWebXR.EnsureInstance();
        SimpleWebXR.SessionStart.AddListener(EnableVRMode);

        using (UnityWebRequest www = UnityWebRequest.Get(Url))
        {
            yield return www.SendWebRequest();
            if (www.result != UnityWebRequest.Result.Success)
            {
                Debug.Log(www.error);
            }
            else
            {
                var str = www.downloadHandler.text;
                if (!string.IsNullOrEmpty(str))
                {
                    var array = JArray.Parse(str);
                    Characters = array.Select(line=>line.ToString()).ToList();
                    Debug.Log(Characters.Count);
                }
            }
        }

        Debug.Log("Ready");
    }

    private void EnableVRMode()
    {
        Debug.Log("VR Enabled");
    }
}

If SimpleWebXR.EnsureInstance(); is moved behind the using 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions