Merging XPS documents causing memory leak
I am using the following code to merge xps documents. I do this in a web app. The code works fine without a problem, however I get a memory leak. With every merge more handles are created and more Heap 2 memory is allocated and will not be taken back, even if I force a garbage collection. ie. GC.Collect( 2 ).
This memory is never freed and eventually the web site runs into SystemOutOfMemory.
Can anyone see something I am doing wrong? Or if they have encountered this problem before, know of a work around?
Code BlockPackage oPackage = null;
XpsDocument oXpsDocument = null;
try {
// New XPS package
oPackage =
Package.Open( sTempFilename, FileMode.Create );oXpsDocument =
new XpsDocument( oPackage, CompressionOption.SuperFast ); FixedDocumentSequence oFixedDocumentSequence = new FixedDocumentSequence(); // Read in old foreach( string sXpsDocument in sXPSFiles ) {
XpsDocument oSourceDocument = new XpsDocument( sXpsDocument, FileAccess.Read ); FixedDocumentSequence oOldFixedDocumentSequence = oSourceDocument.GetFixedDocumentSequence(); foreach( DocumentReference oDocumentReference in oOldFixedDocumentSequence.References ) {
DocumentReference oNewDocReference = new DocumentReference();( oNewDocReference
as IUriContext ).BaseUri = ( oDocumentReference as IUriContext ).BaseUri;oNewDocReference.Source = oDocumentReference.Source;
oFixedDocumentSequence.References.Add( oNewDocReference );
oNewDocReference =
null;}
oSourceDocument.Close();
oSourceDocument = null;
}
XpsDocumentWriter oXpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter( oXpsDocument );
//oXpsDocumentWriter.WriteAsync( oFixedDocumentSequence );
oXpsDocumentWriter.Write( oFixedDocumentSequence );
null;oXpsDocument.Close();
oPackage.Close();
oFixedDocumentSequence =
oXpsDocumentWriter =
null;}
catch( Exception ex ) {// Output exception
}
finally {
if( oXpsDocument != null ) {
null;oXpsDocument.Close();
oXpsDocument =
}
if( oPackage != null ) {
null;oPackage.Close();
oPackage =
}
}
Všechny reakce
- This is a bug in the FixedDocumentSequence. The only *work-around* currently exists is to call UpdateLayout() to at least *one* fixed page...
in your case:
...
FixedPage page = oOldFixedDocumentSequence.DocumentPaginator.GetPage(0).Visual as FixedPage;
page.UpdateLayout();
...
and it should work... old question, but in case someone is running into the same problem... (c;
-Jo

