Bizarre Memory Issues on Standalone Build

So, I have this game that works fine in the Editor. When I create a stand-alone build (OSX) and run it, I get all kinds of bizarre errors like

System.NullReferenceException: Object reference not set to an instance of an object
System.InvalidCastException: Cannot cast from source type to destination type
and the ever so fun InvalidArrayIndex on foreach loops.

  1. Now, believe me, these errors are either incorrect or something very strange is going on, because I know these objects exist, are not null, etc. I have wrapped certain code to test this and, even though the stacktrace points to, say, like 10, I know for a fact on line 9 and 11 that everything is fine, the objects exist, the state is the same, nothing is changing them, etc.

  2. Now, of course, I am using threading (SURPRISE! :stuck_out_tongue: ), but, before you hop on that bandwagon, the stack trace is pointing to objects that are local scope or are never accessed by more than 1 thread. Believe me, while the fundamental issue is obviously related to threading, it’s not because I am mangling objects between threads. Sometimes these are even objects that I’ve taken pains to lock even though they are never accessed elsewhere.

  3. Most, but not all, of these are related to System.Collections.Generic.List objects.

  4. Again, this only happens in a stand-alone build (both regular and dev, x32 and x64). This is 100% reproducible.

  5. All of this codebase was written by me so I am fairly confident there’s not some spurious piece of code somewhere mucking with my objects.

So, my question is, what could possibly be wrong here?

It’s almost as if the GC is culling object on me when I am still using them… except, again, some of these are objects that are not alive for more than 200ms and are entirely local to function scope. My other thought, which is what I am currently leaning towards, is that the stack traces are simply completely wrong due to some threading effect mangling the execution stack when an exception is encountered.

Any ideas or suggestions?

I actually, within the week, have had this start happening to me as well with the arrays in a script managing the lobby of my game. Editor, works perfect. Standalone build, all mucked up with my dev console full of Array exceptions that run the gamut. I literally print out the arrays contents, shows up like it should, then immediately on the next line of code get an exception telling me the array is now empty so I cannot access that index. Best part? I can print out the array on the next line of code after the error and, you guessed it, the array is still correct with all the info it was supposed to have. This causes my lobby to look and function flawlessly in the Editor and then look like a giant pile when its in a Standalone Build.

My Standalone build is for Windows, not OSX, and I’ve not touched threading in my code. I’ll be keeping an eye on this post to see if any proposed solutions may be able to help either of us. :smile:

Interesting… no threads at all, huh?

I’m right in the middle of working on networking, too, meaning, I need to run a build outside the editor to test just about anything (client/server). Ugh.

I’ll see what I can find out tomorrow—I will try rolling back to code from a month or two ago, see if there is any difference. That’s about all I can think of now to try that I haven’t already. I even examined a bunch of IL code today and it looks fine. The stacktraces can’t possibly be accurate.

No threading at least to my knowledge. lol I’m still fairly new to learning C# so when problems like this arise, I get REALLY stuck. Thankfully my lobby is functional enough with the errors to let me work on other things in the meantime while I scour forums like this for a solution.

It’s funny you mention networking as that’s exactly what I’ve been working on so having to do lots of builds for testing as well. It was around the time of implementing the code to sync my player ID array across the network that I started getting these. By any chance does your errors happen with Arrays that are being passed over a network?

I haven’t figured anything out yet but here are some avenues I plan on investigating:

  1. Try disabling garbage collector (not possible to disable it but you can set it to only collect on extremely low memory).

  2. Check my old code branches to narrow down exactly were the bug was introducted.

  3. Double check that the bug is affecting both x32 and x64 builds, on both Win and OSX.

  4. Check if any networking in being used, even if ignored in single-player build. Check the order of RPC calls.

  5. Check order of initialization of game objects’ Awake() and Start(), see if they are different in Editor and Player. Manually set order of execution via Script Execution Order settings.

  6. Check if a NullReferenceException can be triggered by having null objects in the array itself and not just having a null array.

  7. Does the build have all of the required assets? (check Editor.log after building for a list of all files included by Editor, and check the directory structure of the build).

I’d look at the networking and make sure everything the server creates is also created on the client. Easiest way to check would be to use the build as the server and have the editor connect as a client.

So… found another clue… the x32-bit build works fine. The x64 has the problems.

Good thing I double-checked.

Will post more when I find out more.