Problem deserializing 2dim and jagged array..

wyrd

Senior Contributor
Joined
Aug 23, 2002
Messages
1,405
Location
California
I've been trying to track down a deserialization error I've been getting for a while now, and I think I found the cause. The error I was getting is when trying to deserialize from a file.. and yes I'm using StreamingContextStates.File. Here's the error;

"The object with ID 4097 was referenced in a fixup but has not been registered."

The class in which I serialize is nothing more then a two dim array, structs, a bool value, and some strings. The two dim array is made up of structs.

Being confused as I was, I marked the two dim array so it wouldn't serialize. Oddly enough, the deserialization worked after that (after reserializing the altered class of course).

I tried serializing both a jagged array and a regular array; stuff[][] and stuff[,]. Both give the deserialization error. Marking this two dim array to not serialize is not an option, neither is using an ArrayList in replace of it.

Is there any way to fix this? I'm still confused as to why a two dim or jagged array will not serialize and deserialize correctly.

Thanks in advance.
 
More info..

It seems to work when de/serializing in XML format, however the problem is with binary. *boggled* I upload the XML serialized file so you could see what's being serialized, and also below is the relevant code to the problem;

Serialized file: http://www.danpeverill.com/test.xml

C#:
// NOTE: XML and binary serialization code is identical, 
// just replace one formatter for the other.

[NonSerialized]
private Bitmap _areaImages;

[NonSerialized]
private TextureBrush[][] _areaTextures;

private MapArea[][] _map;

private Size _areaSize;
private Size _mapSize;

private Rectangle _camera;

private string _name = "";
private string _desc = "";

[NonSerialized]
private bool _disposed = false;


public static Map Load(string path) 
{
	try {
		FileStream file = new FileStream(path, FileMode.Open);
		StreamingContext context = new StreamingContext(StreamingContextStates.File);
		SoapFormatter formatter = new SoapFormatter(null, context);

		Map map = (Map) formatter.Deserialize(file);
		file.Close();

		return map;
	}
	catch (Exception e) {
		Console.WriteLine(e.Message);
		return null;
	}
}


public static bool Save(Map map, string path) 
{
	try {
		FileStream file = new FileStream(path, FileMode.Create);
		StreamingContext context = new StreamingContext(StreamingContextStates.File);
		SoapFormatter formatter = new SoapFormatter(null, context);

		formatter.Serialize(file, map);

		file.Close();
		return true;
	}
	catch (Exception) {
		return false;
	}
}

Any ideas on how to fix this for binary?
 
Your code looks fine. It appears you're the lucky recipient of BUG 320079. Congratulations!

Seriously though... the binary formatter has a known problem with deserializing structures, arrays of structures and nested structures. I strongly suggest you convert to classes, which is the only true fix I've been made aware of.
 
Back
Top