1

Okay, The post was getting way too long and contained way too much self corrects, so I'm rewriting it from start. If you want to read back, check the changelog.

The latest revision of the code can be found here: http://pastebin.com/KMHVb5gA I'm getting an overflow in system.XML.dll for some reason; It has no stacktrace, and it just stops execution.

I have no idea WHY it overflows, but I Do know it happens after I call Save()

            public bool save(string filename) //causes stack overflow when savenode is IEnumerable (and otherwise it does nothing).
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<savenode>));
                System.IO.FileStream fstream = new System.IO.FileStream(filename, System.IO.FileMode.OpenOrCreate);
                serializer.Serialize(fstream, innerdict);
                fstream.Flush();
                fstream.Close();
                return true;
            }

I'm completely new to making my own IEnumerable classes, so if you see something obvious wrong, please let me know!

The calling code looks like this:

        Console.WriteLine("Commencing XML persistency test");
        CedLib.Persistence.XMLPersistenceDictionary.XMLPersistenceDictionary persistence = new CedLib.Persistence.XMLPersistenceDictionary.XMLPersistenceDictionary(logger); //Works!!
        persistence.Add("test", "testvaluefennecs");
        Console.WriteLine(persistence["test"].obj);
        foreach (var snode in persistence)
        {
            Console.Write("Contents: " + snode.obj);
        }
        persistence.save("test.xml");
        persistence.load("test.xml");
        if (persistence["test"].obj != "testvaluefennecs")
        {
            logger.logerror(new Exception("XML test failed!! Expected 'testvaluefennecs', got: " + persistence["test"].obj));
        }
        else
            Console.WriteLine("XML test success!");

And the output like this:

Commencing XML persistency test
[17:54:09] info: Initialized new XMLPersistence dictionary
New node: test
[17:54:09] Notice: Adding new dictionary item: test
testvaluefennecs
Contents: testvaluefennecs
Process is terminated due to StackOverflowException.

Anyone have an idea? Any suggestion is welcome! I'm completely stuck on this!

[edit] Just found the exact line it overflows at, it's this:

XmlSerializer serializer = new XmlSerializer(typeof(List<savenode>));
10
  • 2
    enable Exceptions in VS like here florianreischl.blogspot.com/2010/01/… to see where it's thrown Commented Nov 30, 2011 at 15:47
  • Doesn't work when it has a stackoverflow, tried it couple of days ago. Commented Nov 30, 2011 at 15:50
  • I know WHERE the error is thrown in MY code, but the actual exception happens outside my code, in the XML serialization library. I'll add the output of the console to show you what i mean. Commented Nov 30, 2011 at 15:51
  • Have you tried another serializer such as DataContractSerializer? Maybe there is an issue with your class, not the serializer. Do other serializers fail? Commented Nov 30, 2011 at 15:58
  • Thanks for the suggestion though, i didn't know about that part of visual studio. Commented Nov 30, 2011 at 15:58

2 Answers 2

1

Ok, this is in no way definitive, but it might help..

It's actually dying after :-

persistence.save("test.xml");

At:-

XmlSerializer serializer = new XmlSerializer(typeof(List<savenode>));

Stacktrace:

...more...stackoverflow... 
System.Xml.dll!System.Xml.Serialization.TypeDesc.CheckSupported() Line 326  C#
System.Xml.dll!System.Xml.Serialization.TypeDesc.CheckSupported() Line 337 + 0xa bytes  C#
System.Xml.dll!System.Xml.Serialization.TypeDesc.CheckSupported() Line 337 + 0xa bytes  C#
System.Xml.dll!System.Xml.Serialization.TypeDesc.CheckSupported() Line 337 + 0xa bytes  C#
System.Xml.dll!System.Xml.Serialization.TypeDesc.CheckSupported() Line 337 + 0xa bytes  C#
System.Xml.dll!System.Xml.Serialization.TypeDesc.CheckSupported() Line 337 + 0xa bytes  C#
System.Xml.dll!System.Xml.Serialization.TypeDesc.CheckSupported() Line 337 + 0xa bytes  C#
System.Xml.dll!System.Xml.Serialization.TypeDesc.CheckSupported() Line 337 + 0xa bytes  C#
System.Xml.dll!System.Xml.Serialization.TypeDesc.CheckSupported() Line 337 + 0xa bytes  C#
System.Xml.dll!System.Xml.Serialization.TypeDesc.CheckSupported() Line 337 + 0xa bytes  C#
System.Xml.dll!System.Xml.Serialization.TypeDesc.CheckSupported() Line 337 + 0xa bytes  C#
System.Xml.dll!System.Xml.Serialization.TypeScope.GetTypeDesc(System.Type type, System.Reflection.MemberInfo source, bool directReference, bool throwOnError) Line 647  C#
System.Xml.dll!System.Xml.Serialization.ModelScope.GetTypeModel(System.Type type = {Name = Cannot evaluate expression because the code of the current method is optimized. FullName = Cannot evaluate expression because the code of the current method is optimized.}, bool directReference) Line 40 + 0x15 bytes  C#
System.Xml.dll!System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(System.Type type = {Name = Cannot evaluate expression because the code of the current method is optimized. FullName = Cannot evaluate expression because the code of the current method is optimized.}, System.Xml.Serialization.XmlRootAttribute root = null, string defaultNamespace = null) Line 159 + 0x23 bytes    C#
System.Xml.dll!System.Xml.Serialization.XmlSerializer.XmlSerializer(System.Type type = {Name = Cannot evaluate expression because the code of the current method is optimized. FullName = Cannot evaluate expression because the code of the current method is optimized.}, string defaultNamespace = null) Line 200 + 0x27 bytes   C#
System.Xml.dll!System.Xml.Serialization.XmlSerializer.XmlSerializer(System.Type type) Line 177  C#
ConsoleApplication4.exe!CedLib.Persistence.XMLPersistenceDictionary.XMLPersistenceDictionary.save(string filename = "test.xml") Line 139 + 0x25 bytes   C#

I think this may be your problem:-

Serialization of ArrayList and Generic List

The XmlSerializer cannot serialize or deserialize the following:

Arrays of ArrayList

Arrays of List(Of T)

From: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

Unfortunately I'm not sure how you can fix it...

Sign up to request clarification or add additional context in comments.

7 Comments

Added the stacktrace to your answer. Should be in the question. Sorry for that.
No worries, makes me look clevererer... ;)
That's odd, In a previous revision of my code i used a List<savenode> as well, and that worked just fine, this is my previous revision: pastebin.com/vZwKdMmc -- Even though the code is rather different, the List<savenode> never changed.. The whole reason i made specdict is because it refused to serialize Dictionary<> -- EDIT: ARRAYS of List<> can NOT be serialized, LIST<> alone however, can.
But thanks for pointing me in the right direction, I'll have some new critical look upon that part again.
Was savenode also an IEnumerable in that version as well though? It's kind of a list of lists, although I don't see why a serialiser couldn't cope with that.
|
0

I GOT IT! A friend of mine pointed me to it, and i have to add that JonB has helped a lot too!

The problem is that savenode was IEnumerable. I thought i needed that, but turns out i don't! The reason the serializer was overflowing was because making savenode enumerable, turned List into an array of lists (which it can't serialize!)

The trick was simply turning savenode non-IEnumerable again, and all worked!

Now to find other, new bugs and swat those in a shorter time span. Sometimes, being programmer is like playing super meat boy; You're stuck in the same level for DAYS but when you finally nail it, you're waking up the neighboars with your joy. (ups lol)

New version of the code for those who are interested: http://pastebin.com/7xQUMAUn

Current calling code, Working:

        Console.WriteLine("Commencing XML persistency test");
        CedLib.Persistence.XMLPersistenceDictionary.XMLPersistenceDictionary persistence = new CedLib.Persistence.XMLPersistenceDictionary.XMLPersistenceDictionary(logger); //Works!!
        persistence.Add("test", "testvaluefennecs");
        persistence["test"].Add("test", "fennecs");
        Console.WriteLine(persistence["test"].obj);
        foreach (var snode in persistence)
        {
            Console.Write("Contents: " + snode.obj);
            foreach (var item in snode.childnodes)
            {
                Console.Write("Contents: " + snode.obj);
            }
        }
        Console.WriteLine("\nSaving dictionary");
        persistence.save("test.xml");
        persistence = null; Console.WriteLine("Nullified dictionary");
        persistence = new CedLib.Persistence.XMLPersistenceDictionary.XMLPersistenceDictionary();
        persistence.load("test.xml");
        Console.WriteLine("Loaded dictionary");
        if (persistence["test"].obj != "testvaluefennecs")
        {
            logger.logerror(new Exception("XML test failed!! Expected 'testvaluefennecs', got: " + persistence["test"].obj));
        }
        else
            Console.WriteLine("XML test success!");

May the code be with you!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.