Windows Build can't create Texture2D on Unity 2019.4 (LTS).

Hi everybody thanks for reading!

I have a project that runs perfectly on the Editor. However, when I make a build it crashes with the error:

UnityEngine.Texture2D:Internal_CreateImpl (From the stack trace)

By adding logs on the project I discvered that it happens when I am calling:

Texture2D tex = new Texture2D(1,1);

Does anyone knows why creating a texture could cause the build to crash?

Thank you so much for the help!
Lucas

Here is the fuction that calls the line above:

private async Task<(int, Texture2D)> LoadTextureAsync(string path, int id, CancellationToken ct)
        {
            UnityWebRequest TextureFileRequest = null;
            try
            {
                Texture2D tex = new Texture2D(1,1);// Decrypt the source file and write it to the destination file.
                MemoryStream destinationStream = new MemoryStream();
              
                Debug.Log(path);
                using (var sourceStream = File.OpenRead(path))
                using (var provider = new AesCryptoServiceProvider())
                {
                    var IV = new byte[Auxiliary.Config.EncryptionIV.Length];
                    sourceStream.Read(IV, 0, IV.Length);
                    using (var cryptoTransform = provider.CreateDecryptor(Auxiliary.Config.EncryptionKey, IV))
                    using (var cryptoStream = new CryptoStream(sourceStream, cryptoTransform, CryptoStreamMode.Read))
                    {
                        cryptoStream.CopyTo(destinationStream);
                        tex.LoadImage(destinationStream.ToArray());
                        return (id, tex);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError("Error loading media" + e);
                if (TextureFileRequest != null)
                    TextureFileRequest.Dispose();
                return (id, null);
            }
        }

Here is part of the stack trace:

========== OUTPUTTING STACK TRACE ==================

0x00007FFC58B9C159 (UnityPlayer) UnityMain
0x00007FFC58B927EF (UnityPlayer) UnityMain
0x00007FFC58B7011E (UnityPlayer) UnityMain
0x00007FFC58E8340A (UnityPlayer) UnityMain
0x000001FBF46DC7A0 (Mono JIT Code) (wrapper managed-to-native) UnityEngine.Texture2D:Internal_CreateImpl (UnityEngine.Texture2D,int,int,int,UnityEngine.Experimental.Rendering.GraphicsFormat,UnityEngine.Experimental.Rendering.TextureCreationFlags,intptr)
0x000001FBF46DC603 (Mono JIT Code) UnityEngine.Texture2D:Internal_Create (UnityEngine.Texture2D,int,int,int,UnityEngine.Experimental.Rendering.GraphicsFormat,UnityEngine.Experimental.Rendering.TextureCreationFlags,intptr)
0x000001FBF46DA3AB (Mono JIT Code) UnityEngine.Texture2D:.ctor (int,int,UnityEngine.TextureFormat,int,bool,intptr)
0x000001FBF46DA283 (Mono JIT Code) UnityEngine.Texture2D:.ctor (int,int)
0x000001FBF46D9AF3 (Mono JIT Code) VIS.Data.Persistent.DataVault/d__16:MoveNext ()
0x000001FBF46D998B (Mono JIT Code) System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<System.ValueTuple2<int, UnityEngine.Texture2D>>:Start<VIS.Data.Persistent.DataVault/d__16> (VIS.Data.Persistent.DataVault/d__16&)
0x000001FBF46D927B (Mono JIT Code) VIS.Data.Persistent.DataVault:LoadTextureAsync (string,int,System.Threading.CancellationToken)
0x000001FBF463B93B (Mono JIT Code) VIS.Data.Persistent.DataVault/d__13:MoveNext ()
0x000001FBF463ACC3 (Mono JIT Code) System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<bool>:Start<VIS.Data.Persistent.DataVault/<LoadMenuThumbnails>d__13> (VIS.Data.Persistent.DataVault/<LoadMenuThumbnails>d__13&) 0x000001FBF463AB6B (Mono JIT Code) VIS.Data.Persistent.DataVault:LoadMenuThumbnails (System.Threading.CancellationToken) 0x000001FBF463A843 (Mono JIT Code) VIS.Data.DataManager/<LoadHomeThumbnailsAsync>d__27:MoveNext () 0x000001FBF463A733 (Mono JIT Code) System.Runtime.CompilerServices.AsyncTaskMethodBuilder1:Start<VIS.Data.DataManager/d__27> (VIS.Data.DataManager/d__27&)
0x000001FBF463A5F3 (Mono JIT Code) VIS.Data.DataManager:LoadHomeThumbnailsAsync (System.Threading.CancellationToken)
0x000001FBF4639C1B (Mono JIT Code) VIS.Core.AppManager/d__41:MoveNext ()

Is this code running on a thread other than the main thread? I would guess this might be one of those “Unity doesn’t like most Unity objects being touched outside the main thread” situations.

Thanks for the reply PraetorBlue.

I had the same guess.

It is called from the main thread, but it is a Task and per regular C# code it could run on a separate thread.

Weird thing is that it works when I build for Android and for iOS ( I had this running in production for a while now).

I did a small separate test building just a function with an Async Task that creates textures but couldn’t replicate the error (I called it several times).

Keep thinking something is not getting clean or disposed. So I tried calling Dispose on every Task after completed and than a GC.Collect() but no luck.

To be honest I don’t know exactly how Unity handle Tasks under the hood. :frowning:

For anyone still having that issue:
I had that issue for some days, I was trying to get an image from FireBase storage via byte and transfering it to a texture2D; however I had the the same problem creating the texture2D. running the code on main thread resolved it (
ContinueWithOnMainThread
)