WebGL: RuntimeError: memory access out of bounds.

Hello to everybody,
so im facing this error “Runtime Error: memory access out of bounds.” when i run my webGL build on some mobile browsers.


  • These are my tests:
  • Fail: Chrome for android, version
    87.0.4280.101 (latest stable until now);
  • Success: Firefox for android, version
    84.1.1 (latest stable until now);
  • Fail: Safari for iOS (latest stable
    version);
  • Fail: Chrome for iOS (latest stable
    version);
  • Fail: Firefox for iOS (latest stable
    version);
  • On desktop, works correctly on any
    browser.

How do i generate this error:

Just by running a WWW or UnityWebRequest to download a file data like this:

byte[] data = null;
WWW www = new WWW(url);
yield return www;
if (www.error != null)
	throw new Exception(www.error);
else
	data = www.bytes;
        
print(www.text.Length);//Printing the text lenght give the error
print(data.Length);//Printing the bytes lenght doesnt give the error

Note: it happens only with files that are like 10/20MB or heavier, i’ve tried with a file that is less than 1MB and it works correctly on all the mobile browsers.
I already read that a solution should be by increasing the memory allocation / memory heap, but i didnt understand how.

i already tried this and many other ways:

 var maxHeapSize = 256 * 1024 * 1024;
 UnityEditor.PlayerSettings.WebGL.emscriptenArgs = string.Format("-s WASM_MEM_MAX={0} -s ALLOW_MEMORY_GROWTH=1", maxHeapSize);

and this too:

UnityEditor.PlayerSettings.WebGL.memorySize = 1024;

But nothing changes, the error is still showing.
And yes, i tried even with UnityWebRequest but still same error.


Ps: Actually i dont need to print the text lenght, but since i need to access to that string, the error will be given anyway even just for a simple access to the string (i guess its because the string is too large and so i should allocate memory first, BUT HOW?)



Thanks to everyone who can help me.

I’ve finally fixed the same error in my project. Here is what i did:

  1. Narrow down the line of code that crashes WebGL.

    Go through the painfull process of pinpointing the line of code that
    is the source of the error. I had this “luck” that error occured
    when I’ve hit button and tried to load UIScene in Addition mode. So
    at first I found the script in the scene which when disabled got rid
    of the crash. Then I’ve repeated the steps and digged deeper into
    lines. After 2 hours of builds, comments etc I’ve found that the code
    was not working was setting the new color for UnityEngine.UI.Image.
    Which was pretty weird because in playmode everything worked ok.

  2. Find solution.

I think my solution might not be universal but I think there is something going in the Unity gameloop/lifecycle when running WebGL. The source of the problem was that i set the field that was storing my UI.Image in the Start method and then in some function I’ve tried to change the color.
Property was not exposed in inspector.

public class ModuleUI : MonoBehaviour
{
    Image qualityBG;

    void Start()
    {
        qualityBG = GetComponent<Image>();
    }

then this line below was the cause of the crash

public void Set(Module module)
    {
        ...
        qualityBG.color = module.Quality.ToColor();
   }
  1. Fixing - so I’ve added [SerializeField] attribute to the field and set it up in editor, dragged the Image into the inspector slot. An it fixed it. I suspect that WebGL build might perform some methods in different order, or maybe the multipleScene loaded together might to do with it. I’m not sure but the field was not set properly with previous code fot WebGL build.

Bonus:
I’ve also fixed some issues that was not critical (not crushing WebGL game) but not working properly as expected in playmode. This also has got to do with trying to set some object in Start() method and then doing something on them. The collection was not set up (probably null) in WebGL.

EffectUI[] effects;
Start()
{
    effects = GetComponentsInChildren<EffectUI>();
}
void HideAllEffects()
    {
        if (effects != null)
            for (int i = 0; i < effects.Length; ++i)
                effects*.gameObject.SetActive(false);*

}
And this also started working when I’ve added [SerializeField] to effects and hook it up in the scene…
Hope this will help some of you guys & gals!

I have a similar problem now. If you get an answer, then beep.

Up .

in my case the problem was solved with the option “full without stacktrace” in the “enable exception field”
194219-untitled.jpg

Same problem Unity 2019.4.39f1 & 2022.2.0a13
“RuntimeError: Out of bounds memory access”
Android, Chrome Desktop works, IOS fails

The solution was to update to iOS 15.5.