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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

DownloadHandlerTexture Crash

Discussion in 'Editor & General Support' started by SevenPointRed, Jan 26, 2018.

  1. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    199
    Unity 2017.3.0p3, Android build, Mono ( il2cpp same issue )
    Crash report from Unitys performance reporting.

    Code (CSharp):
    1. Thread 0 (crashed)
    2. 0   libunity.so                         <symbols missing for uuid: a3d8672ba14ad199de7c22043ad3cb885ba0f06a>
    3. 1   libunity.so                         <symbols missing for uuid: a3d8672ba14ad199de7c22043ad3cb885ba0f06a>
    4. 2   .                                   <unknown>
    5. 3   Mono Runtime                        at (wrapper managed-to-native) UnityEngine.Networking.DownloadHandlerTexture.InternalGetTextureNative () <0xffffffff>
    6. 4   Mono Runtime                        at UnityEngine.Networking.DownloadHandlerTexture.InternalGetTexture () <0x0009c>
    7. 5   Mono Runtime                        at UnityEngine.Networking.DownloadHandlerTexture.get_texture () <0x0001b>
    8. 6   Mono Runtime                        at UnityEngine.Networking.DownloadHandlerTexture.GetContent (UnityEngine.Networking.UnityWebRequest) <0x0002f>
    Code below: none of the log methods are called and no exceptions are raised.

    Code (CSharp):
    1. using (var www = UnityWebRequestTexture.GetTexture(uri, false))
    2.                         {
    3.                             yield return www.SendWebRequest();
    4.                             if (www.isHttpError || www.isNetworkError)
    5.                             {
    6.                                 log.Warn("Fail PreFetchImage (unity error): " + uri);
    7.                             }
    8.                             else
    9.                             {
    10.                                 try
    11.                                 {
    12.                                     var texture = DownloadHandlerTexture.GetContent(www);
    13.                                     if (texture != null)
    14.                                     {
    15.                                         log.Info("Success PreFetchImage: " + uri);
    16.                                     }
    17.                                     else
    18.                                     {
    19.                                         log.Warn("Fail PreFetchImage (no texture): " + uri);
    20.                                     }
    21.                                 }
    22.                                 catch( Exception e)
    23.                                 {
    24.                                     log.Error(e);
    25.                                 }
    26.                             }
    27.                         }
     
  2. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    199
    I've done separate builds for x86 and ARMv7, same issue.
     
  3. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    Do you use Resources.UnloadUnusedAssets anywhere in your code? I've found that this corrupts the usage of DownloadHandlerTexture. You can circumvent this either by ensuring you don't call this method while a texture is downloading, or to change to using the generic UnityWebRequest and creating a texture from the returned byte array.

    Neither are ideal, I'm hoping this will get fixed in the near future. It appears to be in the issue tracker already.
     
  4. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    199
    I do yes, though I've changed my most recent version to do exactly what you said, I've not seen the crash since so it seemed like that was the issue.
     
  5. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    DownloadHandlerTexture creates a texture from downloaded data automatically, but if you switch scene or unload unused assets, that texture can be destroyed as unused. Depending on at what moment this happens, the behavior is non-deterministic.
    Bottom line: don't use DownloadHandlerTexture if you do unload unused assets or switch scenes while download is going.
     
  6. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    199
    Would be good to have that in the docs, I spent quite a long time hunting that bug.
     
    mkkim_vive likes this.
  7. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,064
    I am having this issue as well... but in my case the DownloadHandlerTexture is crashing at the same point (in the editor) and sometimes it doesn't crash but gets a random texture from a random memory location. I've had some weird textures, mostly it crashes the editor though. Happens with Unity 2017.3.0p3 but not with Unity 2017.2.0p3.

    I also have an issue tracker running because of a different issue with DownloadHandlerTexture.GetContent(request) not returning a texture and throws an assertion error. It only happens when a scene has been loaded asynchronously.
    https://issuetracker.unity3d.com/is...loadhandlertexture-getcontent-unitywebrequest
    My work around of that issue was to do a for loop to use GetContent multiple times if the first one fails, the second time it mostly succeeds in getting the texture. But now the work around doesn't seem to work anymore and the Editor crashes quite often due to this bug. Using the bytes from the request to create the texture manually causes a freeze / lagspike because it is a synchronous process. Whereas DownloadHandlerTexture does do the job better but with this bug I cannot upgrade to Unity 2017.3

    TLDR; I don't use Resources.UnloadUnusedAssets nor am I switching scenes while downloading.

    Do you happen to have a link to the issue tracker that you mentioned?
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    DownloadHandlerTexture should only be used when entire download happends within bounds of the same scene and you don't unload unused asset while download is happening. If that's not the case, use DownloadHandlerBuffer and create the texture manually.
     
    Peter77 likes this.
  9. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,064
    What happens in my project is
    I am in Scene A and I go to Scene B using SceneManager.LoadSceneAsync
    in Scene B there is a script that initiates a download request of the texture (void Start() which starts a coroutine) using UnityWebRequestTexture.GetTexture(url, true)
    After the request is done I use DownloadHandlerTexture.GetContent to get the texture. But it fails occasionally.

    So basically the request and get content of the texture happens in SceneB. I do not use UnloadUnusedAssets at that time.
    Then occasionally I receive an InstanceID != InstanceID.None assertion error and the texture is null.
    If I use GetContent a second time (basically I put a for loop around it until GetContent gives me a texture.) I receive a texture or run out of tries which is 5 times. In Unity 2017.2.0p3 this workaround works fine, in Unity 2017.3.0p3 and p4 it crashes because it is trying to get a texture from memory which is inaccessible.

    But whenever I load Scene B Synchronous using SceneManager.LoadScene, the textures load normally without any problem. No assertion errors... no crashes.

    I have tested this issue multiple times now with Unity 2017.3p3 and p4.
    Scene loading both sync and async.
    This issue seems to be a bit linked together with Asynchronous scene loading.

    Is it possible that when SceneManager.LoadSceneAsync is used the Resources.UnloadUnusedAssets is running while the next scene has been loaded? Hence my problem in a script Start method to download a texture but Resources.UnloadUnusedAssets still running and messing with the UnityWebRequestTexture?

    it would explain a lot if that were the case.
     
  10. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    I believe this is a known bug.
     
  11. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,064
    do you maybe have a link to the issue tracker for this, if it is a known bug?
     
  12. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    199
    What requires a known bug to get put on the "Known issues" section of release notes?
     
  13. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,064
    @Aurimas-Cernius Any update on this? I cannot find the issue on the issue tracker which you believed to be a known bug. I would like to track the progress for this issue as it is quite breaking our project.
     
  14. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    I ended up switching back to the generic UnityWebRequest class which isn't ideal in an enterprise-focused data streaming applications (i.e. constant web requests) in combination with a WebGL build, at least until it is more feasible to confirm all resources are cleaned up explicitly without relying on Resources.UnloadUnusedAssets.

    @MaskedMouse The closest issue I could find was this one. Although I did submit a crash ticket back in November which I received a similar reply to UnityWebRequest + Resources.UnloadUnusedAssets "not being supported" (though this didn't crash Unity nor throw any assertions in 2017.1 and earlier)

    https://issuetracker.unity3d.com/is...loadhandlertexture-getcontent-unitywebrequest

    Edit: Apologies, I just realised you posted a comment on that!
     
  15. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,064
    Yeah thats the bug report I made, I linked it in this thread as well. Using SceneManager.LoadScene instead of LoadSceneAsync is a workaround for the issue. Terrible workaround but it is one.
    When using LoadSceneAsync it should wait until Resources.UnloadUnusedAssets is done before actually using the new scene. Then we wouldn't have the problem with UnityWebRequest being cleaned up while it is in use causing errors and even crashes.
     
  16. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    199
    I have the issue and I have a single scene game. Downloading a texture should hold a strong reference to it, so that it's not cleaned up by Resources.UnloadUnusedAssets.
     
  17. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    If you call Resources.UnloadUnusedAssets while downloading is still happening, you risk destroying the texture that is still being created, as texture creation happens on other thread.
    DownloadHandlerTexture should not be used if you are using Resources.UnloadUnusedAssets or switching scenes.
     
  18. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    199
    Yeah, we have been told. But isn't that a bug?
     
  19. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    We don't consider it to be a bug. DownloadHandlerTexture gives you performance by creating texture in background, but this comes at a price of not being allowed to switch scenes or unload unused assets.
     
  20. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    199
    In that case add a warning to the docs that it doesn't work with async scene loading or Resources.UnloadUnusedAssets, I spent ages chasing this "issue". The docs don't mention it will crash your game if you don't use it right.
     
    Menion-Leah likes this.
  21. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    199
    Just wondering if you are going to add the warning i mentioned to the docs?
     
    Menion-Leah likes this.
  22. WereVarg

    WereVarg

    Joined:
    Apr 9, 2012
    Posts:
    20
    In the Unity 2017 it was throwing nulls and we managed to handle it with retries in coroutines. In the 2018 it just crashes. Using regular webrequest and downloadhandlerbuffer till the better times come. Sacrificing the performance.
     
  23. cliffordr

    cliffordr

    Joined:
    May 4, 2018
    Posts:
    3
    There is a work around for this.
    Cache the texture (static ref or don't destroy instance) as soon as the texture gets downloaded.
    So that even if the Resources.UnloadUnusedAssets is called it doesn't clear the texture in webrequest download handler.
     
  24. Fungi

    Fungi

    Joined:
    Nov 12, 2012
    Posts:
    1
    Glad I finally found this thread. This also took me about 3 days to finally track down. Was only happening on Android and very inconsistently because of the timing of LoadSceneAsync. There should really be something in the docs about this causing an issue.
     
  25. Andrey-Postelzhuk

    Andrey-Postelzhuk

    Joined:
    Nov 26, 2013
    Posts:
    75
    Really scary behaviour of UnityWebRequest.
    Without this thread It should be days to fix it.
    So my workaround is:
    1. Use UnityWebRequest.Get() instead of UnityWebRequestTexture.GetTexture()
    2. Get byte[] data from request (UnityWebRequest.downloadHandler.data)
    3. Use ImageConversion.LoadImage to create a texture.
    4. After using texture should be destroying manually (Object.Destroy(texture))
     
    Last edited: Oct 17, 2018
  26. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    This behavior is no longer true. It won't crash anymore in latest Unity releases.
     
  27. Andrey-Postelzhuk

    Andrey-Postelzhuk

    Joined:
    Nov 26, 2013
    Posts:
    75
    @Aurimas-Cernius Thanks for response. Our project is on the unity version 2017.3.0f3. Have one more reason to update.
     
  28. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,064
    The issue is still stated as active though.
    Just to be clear for anyone looking for the fixes or this thread, which exact versions of unity is this issue fixed?
     
    FaberVi and TriggerAndrew like this.
  29. FaberVi

    FaberVi

    Joined:
    Nov 11, 2014
    Posts:
    145
    ... In unity 2019.2.15f1 crash.
     
  30. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    Stack trace please?
     
  31. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    Does this mean that we can freely use the DownloadHandlerTexture class with async scene loading now and it will download textures properly? Or does that mean it just won't crash, but the textures will still fail to download?
     
  32. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    It will work now. Note however, that keeping donwload handler is not the proper way to keep the texture alive. If texture is already accessed and then the scene is switched, the texture might be destroyed and accessing it again from download handler will recreate it (synchronously).
     
  33. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    Thank you.

    I immediately pull the texture out of the request and store it upon completion.

    I was also reading through the code of DownloadHandlerTexture and did see that internally through one code path it seemed to do the standard .LoadImages call taking in a png byte array. Which made me wonder. But I think you just answered my question, but just to make sure I have it right.

    If I submit the request and then call DownloadHandlerTexture.texture before any Resources.UnloadUnusedAssets or asynchronous scene loading task, then it will grab a texture that was generated on a background worker thread that did not disrupt the main? But if I call it after UnloadUnusedAsset or ascynchronous scene loading, then it will recreate the texture via the .LoadImages method synchronously as a fail safe?

    Also, I switched my application to use DownloadHandlerTexutre and it seemed to improve perf some, maybe 30% over just synchronous .LoadImages. Not as much as I hoped. Does that seem correct? I assume even if it's processing the png asynchronously on a background thread, the frame hit is it having to upload it to the GPU from the main thread?
     
    Last edited: Jan 16, 2020
  34. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    Almost, but not exactly. It creates a texture that will survive Unloads until you get the texture from texture property. After you got texture out of handle it starts working like a normal texture and will be destroyed if not used in scene. The fails safe code for applicable only to the case when texture was already taken from handler and is being queried again with Unload in between.

    Yes, that's exactly how it works.
     
  35. eatbuckshot

    eatbuckshot

    Joined:
    Jan 10, 2016
    Posts:
    24
    We were still hitting crashes in 2019.4.0f1 using the DOTS Sample project and loading several dozen images in, paced 10 at a time every few frames.

    When it doesn't crash, there's a default debug log that says the scene finished loading (checking isdone) but when it does crash the player log shows that it never finished loading.

    There are various stack traces that aren't always exactly the same but at least usually end up somewhere in TextureStreamingData, in the downloadhandlertexture. It's non-deterministic as mentioned, and we've been running a automated testing script to restart the app until it crashes. Sometimes the stack trace is some other render/material related method.

    statisically, we seem to be encountering a crash 1 out of every 20 or so launches, sometimes even several times in a row

    For now we're using the suggestion from above:
    > Use [B]UnityWebRequest.Get()[/B] instead of [B]UnityWebRequestTexture.GetTexture()[/B]

    Using this we haven't encountered a rendering related crash after 1500 or so runs (only one related to burst)



     
  36. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    Could you report the bug for this with the repro project? This crash isn't what we've seen before.
     
  37. eatbuckshot

    eatbuckshot

    Joined:
    Jan 10, 2016
    Posts:
    24

    I've tried to submit it but the project (5gb) is so large i'm not sure if it succeeded in uploading.
    upload_2020-12-17_22-1-52.png
     
    Last edited: Dec 24, 2020
  38. eatbuckshot

    eatbuckshot

    Joined:
    Jan 10, 2016
    Posts:
    24
  39. Frimus

    Frimus

    Joined:
    May 28, 2014
    Posts:
    6
    Thankyou it works
     
  40. Menion-Leah

    Menion-Leah

    Joined:
    Nov 5, 2014
    Posts:
    189
    It looks like this report has been successfully reproduced, and then it has been dismissed because "it doesn't reproduce so often". I didn't even know that was a possibility.

    After more than 5 years, the issue is still there, no decent workaround is provided, and the documentation still lacks any kind of mention about it ("For use cases where you wish to download an image via HTTP and use it as a Texture within Unity, usage of this class is strongly recommended.").

    Unity 2020.3.46f1, no explicit call on UnloadUnusedAssets() nor GC.Collect(), just loading scenes asyncronously.