(Apologies for the long post, but I can’t think of any way to explain this in a shorter way!)
I am an experienced programmer, but this is my first project in Unity. I have been mainly testing on a Windows build but I periodically check out the Android build to make sure I don’t have any obvious issues. The Windows build is 100% solid: no errors, no crashes, no glitches. Unfortunately when I tried out the Android build for a first time in a few days I started getting crashes, exceptions, and other unexpected behavior.
TL;DR: The things I’m seeing look like a memory corruption issue, but in the Unity code and not mine. I can’t think of how my simple C# code could manage to blow up the internals of Unity. I’ve tried everything I can think of and I need help on how I can debug this better.
The most common type of issue I see is a NullReferenceException being raised from deep within the Unity codebase. Sometimes this is from me calling a something like Debug.Log, but sometimes it’s completely independent of my code (it looks like it’s being called from Unity’s own rendering loop or something like that).
This is the “fun” kind of issue that moves around as you make changes. For example, I was getting a NullReferenceException from a part of my code where I was certain nothing was null. To make sure I didn’t miss anything, I added several lines validating the objects as well as some things like: Debug.Log(“Finished checks”). The next run, the NullReferenceException moved to one my new Debug.Log statements - despite the fact that I was only logging a string literal with no object references!
Here’s another example. I have an assertion to validate an int variable ‘powerOfTen’:
Assert.IsTrue(powerOfTen < 9999);
When powerOfTen is too high, I should see the following in the log:
AssertionException: Assertion failure. Value was False
Expected: True
This happens 100% of the time on Windows, and some of the time on Android. However, when the Android problem arises, it seems like something has happened to clobber the Unity Engine itself because the same Assert call produces the following in the log:
NullReferenceException: Object reference not set to an instance of an object
at (wrapper stelemref) object:stelemref (object,intptr,object)
at UnityEngine.Assertions.AssertionMessageUtil.GetMessage (System.String failureMessage, System.String expected) [0x00012] in /Users/builduser/buildslave/unity/build/Runtime/Export/Assertions/Assert/AssertionMessageUtil.cs:20
I’ve also seen this kind of error from the same Assert:
Couldn't extract exception string from exception (another exception of class 'NullReferenceException' was thrown while processing the stack trace): 0 at ./Runtime/ScriptingBackend/Mono/ScriptingApi_Mono.cpp:564 (1, 0, 0)
I have tried everything I can think of: remote debugging, adding Debug.Log lines, commenting out sections of code, etc. Every time I think I’m on the track of narrowing the issue down, it manifests itself in another place . And because these errors frequently occur on Unity calls like Debug.Log, I feel like I’m only complicating the issue by adding more.
Anyone have any suggestions as to how I could debug this better?
A few other notes:
- These problems appear both on a real Android phone and on the Android emulator.
- These problems appear whether I install the APK manually (and run it independently of the Unity Editor) or if I install & run the APK through “Build and Run” in the Editor.
- These problems never appear when I run on Windows. I’ve tried both running from the Unity Editor, and creating a standalone .exe and running that.
- I’ve tried running the remote profiler to see if it could tell me anything - like if I was somehow running out of memory on the phone. From what I could tell, I never used more than 14MB on devices with 1.5-3GB. However, the app tended to hard crash if I was too “active” in my app (e.g., rapid clicking) while the remote profiler was open. Since this is my first Unity project I don’t know how reliable the remote profiler is, or if the profiler crashes have anything to do with my overall issues.
- I’ve tried several SDK versions in my build, from Android 4.0 up to Android 7.0. They didn’t seem to make much of a difference other than making the problems move around a bit.
- I’d be happy to share more of the code if it would help narrow this down, but since the problem keeps moving I don’t know what to share.