public

Fix for the problem reported in https://gist.github.com/3002649

  • Download Gist
repro-xmlschemaset-memoryleak-fixed.ps1
PowerShell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
function cleanup() {
write-host "Cleaning up..."
remove-item $schemaFile
break
}
 
trap {
cleanup
}
 
 
$TEST_SCHEMA = @'
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:x">
<element name="a" type="string"/>
<element name="b" type="string"/>
<element name="c" type="string"/>
<element name="d" type="string"/>
<element name="e" type="string"/>
<element name="f" type="string"/>
<element name="A" type="x:string"/>
<element name="B" type="string"/>
<element name="C" type="string"/>
<element name="D" type="string"/>
<element name="E" type="y:string"/>
<element name="F" type="string"/>
</schema>
'@
 
Add-Type -ReferencedAssemblies System.Xml -TypeDefinition @"
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Schema;
public class XsdValidator
{
private List<ValidationEventArgs> _errors = new List<ValidationEventArgs>();
 
public IEnumerable<ValidationEventArgs> Validate(string schemaFileName)
{
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += ValidationCallBack;
using (XmlReader reader = XmlReader.Create(schemaFileName)) {
schemaSet.Add(null, reader);
}
schemaSet.Compile();
return _errors;
}
 
private void ValidationCallBack(object sender, ValidationEventArgs e)
{
_errors.Add(e);
}
}
"@
 
$schemaFileName = [System.IO.Path]::GetTempFileName()
 
$TEST_SCHEMA > $schemaFileName
 
$count = 0
 
$N = 10000
 
1..$N |% {
 
try {
$validator = new-object -typename XsdValidator
$validator.Validate($schemaFileName) |% {
$a = $_
$e = $a.Exception
$GLOBAL:count += 1
write-host "#$GLOBAL:count ${schemaFileName}: $($a.Severity) in $($e.SourceUri) at line $($e.LineNumber) column $($e.LinePosition): $($e.Message)"
}
} catch {
write-host "$($schemaFileName): Exception: $_"
}
}
 
write-host

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.