Skip to content

Instantly share code, notes, and snippets.

@FredDeschenes
Last active December 20, 2015 11:19
Show Gist options
  • Save FredDeschenes/6121949 to your computer and use it in GitHub Desktop.
Save FredDeschenes/6121949 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace ObjectClonerCrashDemo
{
class MainClass
{
public static void Main (string[] args)
{
//This runs fine inside MonoDevelop/VisualStudio 2012
Lib.TestRunner.Run();
Console.WriteLine("Done");
Console.Read();
}
}
}
As you can see, it crashes weirdly at line 24 in ObjectExtensions with a NullReferenceException, which is weird since there is a null check on this object on the previous line.
Doing anything to the object (ie: trying to log it) just moves the error inside String's constructor or String.Format.
Surrounding the line with a try/catch and adding a breakpoint in the catch crashes Unity as soon as the breakpoint is hit, but you can step-in up to this point.
You can download a ZIP file containing the VS2012 solution and Unity demo project here if you want to take a look : https://mega.co.nz/#!0AdlibqB!X5M6T07z58rqLHIEpi7JSk5nvAJICocV_DiEdcuhZy0
using UnityEngine;
using System.Collections;
public class TestScript : MonoBehaviour
{
void Start ()
{
//Crash happens inside here in Unity
Lib.TestRunner.Run();
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Lib
{
public static class TestRunner
{
public static void Run()
{
ToClone temp = new ToClone();
temp.shouldWork = 1234;
temp.willCrash = new List<object>() { new object(), new object() };
var clone = ObjectExtensions.Copy(temp);
Debug.Assert(clone.shouldWork == temp.shouldWork);
Debug.Assert(clone.willCrash.Count == temp.willCrash.Count);
}
}
public class ToClone
{
public int shouldWork;
public List<object> willCrash;
}
}
NullReferenceException: Object reference not set to an instance of an object
System.ObjectExtensions.InternalCopy (System.Object originalObject, IDictionary`2 visited) (at c:/Users/fDeschenes/Desktop/ObjectClonerCrashDemo/Lib/ObjectExtensions.cs:24)
System.ObjectExtensions+<>c__DisplayClass3.<InternalCopy>b__0 (System.Array array, System.Int32[] indices) (at c:/Users/fDeschenes/Desktop/ObjectClonerCrashDemo/Lib/ObjectExtensions.cs:34)
System.ArrayExtensions.ArrayExtensions.ForEach (System.Array array, System.Action`2 action) (at c:/Users/fDeschenes/Desktop/ObjectClonerCrashDemo/Lib/ObjectExtensions.cs:91)
System.ObjectExtensions.InternalCopy (System.Object originalObject, IDictionary`2 visited) (at c:/Users/fDeschenes/Desktop/ObjectClonerCrashDemo/Lib/ObjectExtensions.cs:34)
System.ObjectExtensions.CopyFields (System.Object originalObject, IDictionary`2 visited, System.Object cloneObject, System.Type typeToReflect, BindingFlags bindingFlags, System.Func`2 filter) (at c:/Users/fDeschenes/Desktop/ObjectClonerCrashDemo/Lib/ObjectExtensions.cs:60)
System.ObjectExtensions.InternalCopy (System.Object originalObject, IDictionary`2 visited) (at c:/Users/fDeschenes/Desktop/ObjectClonerCrashDemo/Lib/ObjectExtensions.cs:39)
System.ObjectExtensions.CopyFields (System.Object originalObject, IDictionary`2 visited, System.Object cloneObject, System.Type typeToReflect, BindingFlags bindingFlags, System.Func`2 filter) (at c:/Users/fDeschenes/Desktop/ObjectClonerCrashDemo/Lib/ObjectExtensions.cs:60)
System.ObjectExtensions.InternalCopy (System.Object originalObject, IDictionary`2 visited) (at c:/Users/fDeschenes/Desktop/ObjectClonerCrashDemo/Lib/ObjectExtensions.cs:39)
System.ObjectExtensions.Copy (System.Object originalObject) (at c:/Users/fDeschenes/Desktop/ObjectClonerCrashDemo/Lib/ObjectExtensions.cs:19)
System.ObjectExtensions.Copy[ToClone] (Lib.ToClone original) (at c:/Users/fDeschenes/Desktop/ObjectClonerCrashDemo/Lib/ObjectExtensions.cs:66)
Lib.TestRunner.Run () (at c:/Users/fDeschenes/Desktop/ObjectClonerCrashDemo/Lib/TestRunner.cs:14)
TestScript.Start () (at Assets/Scripts/TestScript.cs:9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment