DownloadHandlerTexture Crash

Unity 2017.3.0p3, Android build, Mono ( il2cpp same issue )
Crash report from Unitys performance reporting.

Thread 0 (crashed)
0   libunity.so                         <symbols missing for uuid: a3d8672ba14ad199de7c22043ad3cb885ba0f06a>
1   libunity.so                         <symbols missing for uuid: a3d8672ba14ad199de7c22043ad3cb885ba0f06a>
2   .                                   <unknown>
3   Mono Runtime                        at (wrapper managed-to-native) UnityEngine.Networking.DownloadHandlerTexture.InternalGetTextureNative () <0xffffffff>
4   Mono Runtime                        at UnityEngine.Networking.DownloadHandlerTexture.InternalGetTexture () <0x0009c>
5   Mono Runtime                        at UnityEngine.Networking.DownloadHandlerTexture.get_texture () <0x0001b>
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.

using (var www = UnityWebRequestTexture.GetTexture(uri, false))
                        {
                            yield return www.SendWebRequest();
                            if (www.isHttpError || www.isNetworkError)
                            {
                                log.Warn("Fail PreFetchImage (unity error): " + uri);
                            }
                            else
                            {
                                try
                                {
                                    var texture = DownloadHandlerTexture.GetContent(www);
                                    if (texture != null)
                                    {
                                        log.Info("Success PreFetchImage: " + uri);
                                    }
                                    else
                                    {
                                        log.Warn("Fail PreFetchImage (no texture): " + uri);
                                    }
                                }
                                catch( Exception e)
                                {
                                    log.Error(e);
                                }
                            }
                        }

I’ve done separate builds for x86 and ARMv7, same issue.

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.

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.

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.

Would be good to have that in the docs, I spent quite a long time hunting that bug.

1 Like

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/issues/assertion-failed-m-instanceid-equals-instanceid-none-unityengine-dot-networking-dot-downloadhandlertexture-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?

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.

1 Like

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.

I believe this is a known bug.

do you maybe have a link to the issue tracker for this, if it is a known bug?

What requires a known bug to get put on the “Known issues” section of release notes?

@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.

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)

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

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.

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.

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.

Yeah, we have been told. But isn’t that a bug?

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.

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.

1 Like