Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

App crashes when calling GetPixels on large image?

Discussion in 'Android' started by look001, Jul 5, 2022.

  1. look001

    look001

    Joined:
    Mar 23, 2017
    Posts:
    104
    Hey there,
    when loading large images (5000x5000 pixels), and calling GetPixels on them, it crashes on android. This is a minimal example..
    Code (CSharp):
    1. void Start()
    2.     {
    3.         var path = Path.Combine(Application.persistentDataPath, "test.png");
    4.         byte[] fileData = File.ReadAllBytes(path);
    5.         tex = new Texture2D(2, 2);
    6.         tex.LoadImage(fileData); // auto-resize the texture dimensions.
    7.     }
    8.    
    9.     void Update()
    10.     {
    11.         if (Input.GetMouseButtonDown(0))
    12.         {
    13.             var colors = tex.GetPixels();
    14.             Debug.Log(colors.Length);
    15.         }
    16.     }
    After clicking four times on the screen, the app crashes. I know the images are large, but is that usual?
    Furthermore, how can I catch the crash and notify the user that the image is to large? Maybe it has something to do with the device hardware, so but I don't know the limit for texture on phones. Also I would like to support at least 5000x5000 pixels if possible.

    Here is a snippet of the logcat:
    Code (CSharp):
    1.     Version '2021.3.4f1 (cb45f9cae8b7)', Build type 'Release', Scripting Backend 'mono', CPU 'armeabi-v7a'
    2.     Build fingerprint: 'samsung/beyond1lteeea/beyond1:11/RP1A.200720.012/G973FXXU9EUA4:user/release-keys'
    3.     Revision: '26'
    4.     ABI: 'arm'
    5.     Timestamp: 2022-07-05 16:51:15+0200
    6.     pid: 25813, tid: 26042, name: UnityMain  >>> com.DefaultCompany.TextureCrash <<<
    7.     uid: 10316
    8. signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x10
    9.     Cause: null pointer dereference
    10.         r0  9b230020  r1  00000020  r2  00000010  r3  00000010
    11.         r4  b5e91ec8  r5  b62eb384  r6  b62eb390  r7  000004e2
    12.         r8  00000004  r9  b62eb310  r10 00001388  r11 b62eb358
    13.         ip  00000020  sp  b62eb308  lr  000004e1  pc  b53f7ad0
    14. managed backtrace:
    15.           #00 (wrapper managed-to-native) UnityEngine.Texture2D:GetPixels (UnityEngine.Texture2D,int,int,int,int,int)
    16.           #01 UnityEngine.Texture2D:GetPixels (int) <0xaf>
    17.           #02 UnityEngine.Texture2D:GetPixels () <0x17>
    18.           #03 Test:Update () <0x2f>
    19.           #04 (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
    20.    
    21.         at libunity.0x6acad0(Native Method)
    Help would be really apprechiated!
     
  2. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,832
    Try checking the max texture size on that device with this.
     
  3. look001

    look001

    Joined:
    Mar 23, 2017
    Posts:
    104
    Thats a good ideas. The max texture size is 8192 so it should be ok. Also, calling GetPixels for the first time works without a problem. But after calling it a fourth time in a row, the app crashes. Maybe, the data is not garbage collected properly and the pixels are still stored after an update?
     
  4. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,832
    Sounds more like OOM(out of memory) issue.
     
  5. look001

    look001

    Joined:
    Mar 23, 2017
    Posts:
    104
    Yes, would be nice to check the memory before it happes, but I don't know how. Any ideas?