NetJSON: Encounter InvalidProgramException
I’m testing NetJSON in Unity with this simple code:
struct SimpleObjectStruct
{
public int ID;
public string Name;
public string Value;
}
public void TestSimpleStruct()
{
var settings = new NetJSON.NetJSONSettings();
var data = new SimpleObjectStruct() { ID = 10, Name = "Test", Value = "Tester" };
var json = NetJSON.NetJSON.Serialize(data, settings);
UnityEngine.Debug.Log(json);
var data2 = NetJSON.NetJSON.Deserialize<SimpleObjectStruct>(json, settings);
UnityEngine.Debug.Log(data2);
}
It throws error on var json = NetJSON.NetJSON.Serialize(data, settings);
InvalidProgramException: Invalid IL code in (wrapper dynamic-method) object:DeserializeValueTextReaderSettings (System.IO.TextReader,NetJSON.NetJSONSettings): IL_0006: ldarg.2
System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, System.Boolean throwOnBindFailure, System.Boolean allowClosed) (at <801a4b5b9f964ad7bbc7574456774626>:0)
System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method) (at <801a4b5b9f964ad7bbc7574456774626>:0)
System.Reflection.Emit.DynamicMethod.CreateDelegate (System.Type delegateType) (at <801a4b5b9f964ad7bbc7574456774626>:0)
NetJSON.NetJSON+DynamicNetJSONSerializer`1[T].CreateDeserializerWithTextReaderSettings () (at <b1d0275b3ef44169a919f0dca690658f>:0)
NetJSON.NetJSON+DynamicNetJSONSerializer`1[T]..ctor () (at <b1d0275b3ef44169a919f0dca690658f>:0)
NetJSON.NetJSON+NetJSONCachedSerializer`1[T].GetSerializer () (at <b1d0275b3ef44169a919f0dca690658f>:0)
NetJSON.NetJSON+NetJSONCachedSerializer`1[T]..cctor () (at <b1d0275b3ef44169a919f0dca690658f>:0)
Rethrow as TypeInitializationException: The type initializer for 'NetJSONCachedSerializer`1' threw an exception.
NetJSON.NetJSON.Serialize[T] (T value) (at <b1d0275b3ef44169a919f0dca690658f>:0)
moveDetection.TestSimpleStruct () (at Assets/moveDetection.cs:97)
moveDetection.Start () (at Assets/moveDetection.cs:18)
I’ve also written a class for testing, same error on Serialize function.
public class SimpleObject
{
public int ID { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
// NetJSON
void TestNetJSON()
{
var o = new SimpleObject() { ID = 100, Name = "Test", Value = "Value" };
var output = NetJSON.NetJSON.Serialize(o);
UnityEngine.Debug.Log(output);
var newObject = NetJSON.NetJSON.Deserialize<SimpleObject>(output);
UnityEngine.Debug.Log(newObject);
}
I’m using Unity2021.3.18f1 with .NET Standard 2.1
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 23 (16 by maintainers)
@wmjordan @jckling . I fixed both issues. I will check in soon once i rerun all the other tests.
First issue i was missing “GenerateUpdateCurrent” and this special scenario never got handle since most folks stored complex object in array
The second issue was missing character type in the list of string type, so it did not know how to handle and terminate the reading of the string
Awesome. I just published the fix in 1.4.3 - > https://www.nuget.org/packages/NetJSON
@wmjordan @jckling . I have pushed the fix and included more test from the .net 4 into .net 7 that should valid 131 scenarios.
Could you please test it again and let me know. So I can patch the current version.
I read the post before the above statement and saw that the exception was not
OutOfMemoryExceptionbutIndexOutOfRangeExceptiononly. Things are quite different.If it is
IndexOutOfRangeException, it is usually caused by wrong pointer position calculation. I met with that kind of exception very frequently while I was dealing with some binary byte array deserialization with my own library. I usually catch the exception thrown from the dynamic deserializer and attach the pointer position and the array length information into the exception. So I can verify which byte is causing trouble and determine whether the deserializer should throw the frame away or bubble up the exception.EDIT: array length above should be changed to array segment.
I meant there is a bug in the code and issue is not the data. 👍
OOM might usually be caused by attempting to allocate an array with a negative size number.
It might be how I am doing allocation that might be causing it to do out of memory or the list creation is buggy therefore it is creating an object that is too large causing OOM.
I will need to debug it locally to figure out what is going on.