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>));
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:
May the code be with you!