OutOfMemoryError calling XmlSerializer.Deserialize() - not related to XML size!

Posted by Mike Atlas on Stack Overflow See other posts from Stack Overflow or by Mike Atlas
Published on 2010-05-12T22:26:13Z Indexed on 2010/05/12 22:44 UTC
Read the original article Hit count: 314

This is a really crazy bug. The following is throwing an OutOfMemoryException, for XML snippits that are very short (e.g., <ABC def='123'/>) of one type, but not for others of the same size but a different type: (e.g., <ZYX qpr='baz'/>).

public static T DeserializeXmlNode<T>(XmlNode node)
{
    try
    {
        return (T)new XmlSerializer(typeof(T))
            .Deserialize(new XmlNodeReader(node));
    }
    catch (Exception ex)
    {
        throw; // just for catching a breakpoint.
    }
}

I read in this MSDN article that if I were using XmlSerializer with additional parameters in the constructor, I'd end up generating un-cached serializer assemblies every it got called, causing an Assembly Leak. But I'm not using additional parameters in the constructor. It also happens on the first call, too, so the AppDomain is fresh.

Worse yet, it is only thrown in release builds, not debug builds.

What gives?

© Stack Overflow or respective owner

Related posts about xmlserializer

Related posts about outofmemoryexception