'Out of memory crash caused by update function on Unity/Android/OculusQuest

The update function below is causing some kind of memory build up on my builds and crashing the game about 5 seconds into the scene even though everything seems to work as intended. The code that runs on Android makes a Texture2D from a camera read/writable, and I'm pretty sure that's what's causing the crash, but not sure exactly why. Under the code are the error and crash messages from logcat. The last 6 messages is where it locates the issue in Unity. Seems like it has something to do with texture formatting?

 void Update ()
    {            

            if(USBCamera.GetComponent<USBCamera>().tempTexture2D != null)
            {
               
                texture = USBCamera.GetComponent<USBCamera>().tempTexture2D;
                RenderTexture tmp = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
                Graphics.Blit(texture, tmp);
                RenderTexture previous = RenderTexture.active;
                RenderTexture.active = tmp;
                Texture2D newTexture = new Texture2D (texture.width, texture.height, TextureFormat.RGBA32, false);
                newTexture.ReadPixels(new UnityEngine.Rect(0, 0, tmp.width, tmp.height), 0, 0);
                newTexture.Apply();
                RenderTexture.active = previous;
                Mat imgMat = new Mat (texture.height, texture.width, CvType.CV_8UC4);
                Debug.Log(newTexture);
                Debug.Log(imgMat);
                Utils.texture2DToMat (newTexture, imgMat);
                rgbaMat = imgMat;
               
            }
    }

05-13 15:57:08.166: E/Unity(8845): at OpenCVForUnity.UnityUtils.Utils.fastMatToTexture2D (OpenCVForUnity.CoreModule.Mat mat, UnityEngine.Texture2D texture2D, System.Boolean flip, System.Int32 flipCode, System.Boolean flipAfter, System.Boolean updateMipmaps, System.Boolean makeNoLongerReadable) [0x00020] in <43d3ae013da743a49a36d666656fc3ab>:0

05-13 15:57:08.166: E/Unity(8845): at OpenCVForUnityExample.ArUcoWebCamTextureExample.Update () [0x00507] in <43d3ae013da743a49a36d666656fc3ab>:0

05-13 15:57:08.166: E/Unity(8845):

05-13 15:57:08.166: E/Unity(8845): (Filename: <43d3ae013da743a49a36d666656fc3ab> Line: 0)

05-13 15:57:08.167: E/Unity(8845): OPENGL NATIVE PLUG-IN ERROR: GL_OUT_OF_MEMORY: Not enough memory left to execute command

05-13 15:57:08.219: E/CRASH(8845): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

05-13 15:57:08.219: E/CRASH(8845): Version '2019.4.32f1 (f88bf0bee961)', Build type 'Release', Scripting Backend 'mono', CPU 'armeabi-v7a'

05-13 15:57:08.219: E/CRASH(8845): Build fingerprint: 'oculus/hollywood/hollywood:10/QQ3A.200805.001/26242800497100000:user/release-keys'

05-13 15:57:08.219: E/CRASH(8845): Revision: '0'

05-13 15:57:08.219: E/CRASH(8845): ABI: 'arm'

05-13 15:57:08.219: E/CRASH(8845): Timestamp: 2022-05-13 15:57:08-0600

05-13 15:57:08.219: E/CRASH(8845): pid: 8845, tid: 8868, name: UnityMain >>> com.DefaultCompany.MetaplexTest <<<

05-13 15:57:08.219: E/CRASH(8845): uid: 10090

05-13 15:57:08.219: E/CRASH(8845): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0

05-13 15:57:08.219: E/CRASH(8845): Cause: null pointer dereference

05-13 15:57:08.219: E/CRASH(8845): r0 dcedb200 r1 003fffe9 r2 00000001 r3 003fffdc

05-13 15:57:08.219: E/CRASH(8845): r4 00000000 r5 00000000 r6 00000010 r7 00130000

05-13 15:57:08.219: E/CRASH(8845): r8 0012c004 r9 e762e260 r10 0012c014 r11 00001d10

05-13 15:57:08.219: E/CRASH(8845): ip 000000ff sp bf77ab50 lr bfb2f2a1 pc c01fcf2a

05-13 15:57:08.219: E/CRASH(8845): backtrace:

05-13 15:57:08.219: E/CRASH(8845): #00 pc 00971f2a /data/app/com.DefaultCompany.MetaplexTest-DmxhjYx_T-KZArPBvhPzxw==/base.apk

05-13 15:57:08.219: E/CRASH(8845): #01 pc 002a429d /data/app/com.DefaultCompany.MetaplexTest-DmxhjYx_T-KZArPBvhPzxw==/base.apk

05-13 15:57:08.219: E/CRASH(8845): #02 pc 002a0a93 /data/app/com.DefaultCompany.MetaplexTest-DmxhjYx_T-KZArPBvhPzxw==/base.apk

05-13 15:57:08.219: E/CRASH(8845): #03 pc 002a0e39 /data/app/com.DefaultCompany.MetaplexTest-DmxhjYx_T-KZArPBvhPzxw==/base.apk

05-13 15:57:08.219: E/CRASH(8845): #04 pc 002a0de5 /data/app/com.DefaultCompany.MetaplexTest-DmxhjYx_T-KZArPBvhPzxw==/base.apk

05-13 15:57:08.219: E/CRASH(8845): #05 pc 0039a65d /data/app/com.DefaultCompany.MetaplexTest-DmxhjYx_T-KZArPBvhPzxw==/base.apk

05-13 15:57:08.219: E/CRASH(8845): #06 pc 0009ee49 /data/app/com.DefaultCompany.MetaplexTest-DmxhjYx_T-KZArPBvhPzxw==/base.apk

05-13 15:57:08.219: E/CRASH(8845): #07 pc 003091f7 /data/app/com.DefaultCompany.MetaplexTest-DmxhjYx_T-KZArPBvhPzxw==/base.apk

05-13 15:57:08.219: E/CRASH(8845): #08 pc 002e6c71 /data/app/com.DefaultCompany.MetaplexTest-DmxhjYx_T-KZArPBvhPzxw==/base.apk

05-13 15:57:08.219: E/CRASH(8845): #09 pc 002e7063 /data/app/com.DefaultCompany.MetaplexTest-DmxhjYx_T-KZArPBvhPzxw==/base.apk

05-13 15:57:08.219: E/CRASH(8845): #10 pc 002e6e23 /data/app/com.DefaultCompany.MetaplexTest-DmxhjYx_T-KZArPBvhPzxw==/base.apk

05-13 15:57:08.219: E/CRASH(8845): #11 pc 002d0d43 /data/app/com.DefaultCompany.MetaplexTest-DmxhjYx_T-KZArPBvhPzxw==/base.apk

05-13 15:57:08.219: E/CRASH(8845): #12 pc 0046e03d /data/app/com.DefaultCompany.MetaplexTest-DmxhjYx_T-KZArPBvhPzxw==/base.apk

05-13 15:57:08.219: E/CRASH(8845): #13 pc 0000bc52 anonymous:bd347000 ****05-13 15:57:08.219: E/CRASH(8845): managed backtrace:

05-13 15:57:08.219: E/CRASH(8845): #00 (wrapper managed-to-native) UnityEngine.Texture2D:Internal_CreateImpl (UnityEngine.Texture2D,int,int,int,UnityEngine.Experimental.Rendering.GraphicsFormat,UnityEngine.Experimental.Rendering.TextureCreationFlags,intptr)

05-13 15:57:08.219: E/CRASH(8845): #01 UnityEngine.Texture2D:Internal_Create (UnityEngine.Texture2D,int,int,int,UnityEngine.Experimental.Rendering.GraphicsFormat,UnityEngine.Experimental.Rendering.TextureCreationFlags,intptr) <0x5f>

05-13 15:57:08.220: E/CRASH(8845): #02 UnityEngine.Texture2D:.ctor (int,int,UnityEngine.TextureFormat,int,bool,intptr) <0xe3>

05-13 15:57:08.220: E/CRASH(8845): #03 UnityEngine.Texture2D:.ctor (int,int,UnityEngine.TextureFormat,bool) <0x67>

05-13 15:57:08.220: E/CRASH(8845): #04 OpenCVForUnityExample.ArUcoWebCamTextureExample:Update () <0x18b>

05-13 15:57:08.220: E/CRASH(8845): #05 (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)****



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source