Bug 8098 - DataContractSerializer attempts to serialize static members.
Summary: DataContractSerializer attempts to serialize static members.
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: WCF assemblies (show other bugs)
Version: master
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-10-31 10:08 UTC by Roman
Modified: 2014-03-26 17:10 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Class file that causes an exception in Mono but not in .Net (657 bytes, application/octet-stream)
2014-03-26 17:10 UTC, Matthew Gatland
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 8098 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
NEW

Description Roman 2012-10-31 10:08:36 UTC
I have very simple struct:

public struct MyKey : IComparable<MyKey>, IEquatable<MyKey>
{
	public static readonly MyKey Empty = new MyKey(-1L);
		
	long m_value;

	public long Value { get; set; }

	// Some code skipped
}


When i try to serialize this struct to FileStream, program hangs and i have to kill it. And resulting file has size of tens of gigabytes.
File consists of something like this <MyKey ...><Empty><Empty><Empty><Empty><Empty><Empty><Empty><Empty>...

When i try to serialize it to MemoryStream, i get this exception:

Stack overflow: IP: 0x598bec, fault addr: (nil)
Stacktrace:
  at <unknown> <0xffffffff>
  at System.Reflection.MonoField.GetValue (object) <0x0006e>
  <...>
  at System.Runtime.Serialization.SerializationMap.SerializeNonReference (object,System.Runtime.Serialization.XmlFormatterSerializer) <0x00146>
  at System.Runtime.Serialization.SerializationMap.Serialize (object,System.Runtime.Serialization.XmlFormatterSerializer) <0x000e2>
  at System.Runtime.Serialization.XmlFormatterSerializer.Serialize (System.Type,object) <0x005ca>
  at System.Runtime.Serialization.SerializationMap.SerializeNonReference (object,System.Runtime.Serialization.XmlFormatterSerializer) <0x001b3>
  at System.Runtime.Serialization.SerializationMap.Serialize (object,System.Runtime.Serialization.XmlFormatterSerializer) <0x000e2>
  at System.Runtime.Serialization.XmlFormatterSerializer.Serialize (System.Type,object) <0x005ca>
  at System.Runtime.Serialization.SerializationMap.SerializeNonReference (object,System.Runtime.Serialization.XmlFormatterSerializer) <0x001b3>
...
<over 9000 repeating lines>
...
(object,System.Runtime.Serialization.XmlFormatterSerializer) <0x000e2>
  at System.Runtime.Serialization.XmlFormatterSerializer.Serialize (System.Type,object) <0x005ca>
  at System.Runtime.Serialization.XmlFormatterSerializer.Serialize (System.Xml.XmlDictionaryWriter,object,System.Type,System.Runtime.Serialization.KnownTypeCollection,bool,int,string,bool,System.Runtime.Serialization.DataContractResolver,System.Runtime.Serialization.DataContractResolver) <0x0007b>
  at System.Runtime.Serialization.DataContractSerializer.WriteObjectContent (System.Xml.XmlDictionaryWriter,object) <0x00093>
  at System.Runtime.Serialization.XmlObjectSerializer.WriteObject (System.Xml.XmlDictionaryWriter,object) <0x00034>
  at System.Runtime.Serialization.DataContractSerializer.WriteObject (System.Xml.XmlWriter,object) <0x0003d>
  at System.Runtime.Serialization.XmlObjectSerializer.WriteObject (System.IO.Stream,object) <0x00040>
  at TestXmlMono.Program.Main (string[]) <0x001e6>
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <0xffffffff>


This does not happen on MS.Net. Empty field is ignored.
Comment 1 Roman 2012-11-01 03:30:39 UTC
IgnoreDataMemberAttribute can be added to static field as a workaround.
Comment 2 Matthew Gatland 2014-03-26 17:00:28 UTC
I just came across this bug while using SFML.Net and serializing a Color object. 

The exception was: Circular reference of an object in the object graph was found: 'Caves.code.SaveSystem+Color' 

Static fields should be ignored, but the static field in a struct is being serialized.

Simplified code to reproduce: call SaveSystem.Test()

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;

namespace Caves.code
{
	class SaveSystem
	{
		struct Color
		{
			public Color(string name)
			{
				this.name = name;
			}

			public static readonly Color Blue = new Color("Blue");
			public string name;
		}

		private static Type[] types = { typeof(Color) };

		public static void Test()
		{
			Color obj = new Color();
			var serializer = new DataContractSerializer(obj.GetType(), types, Int32.MaxValue, false, true, null);
			using (var writer = new StringWriter())
			using (var stm = new XmlTextWriter(writer))
			{
				serializer.WriteObject(stm, obj);
				Console.WriteLine(writer.ToString());
			}
		}
	}
}
Comment 3 Matthew Gatland 2014-03-26 17:10:00 UTC
Created attachment 6416 [details]
Class file that causes an exception in Mono but not in .Net