MSDN > 論壇首頁 > XML Paper Specification (XPS) > Merging XPS documents causing memory leak
發問發問
 

問題Merging XPS documents causing memory leak

  • 2007年10月18日 下午 01:59Keith Ball 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    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 Block

    Package 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 );

     

    oXpsDocument.Close();

    oPackage.Close();

     

    oFixedDocumentSequence = null;

    oXpsDocumentWriter = null;

    } catch( Exception ex ) {

    // Output exception

    } finally {

    if( oXpsDocument != null ) {

    oXpsDocument.Close();

    oXpsDocument = null;

    }

    if( oPackage != null ) {

    oPackage.Close();

    oPackage = null;

    }

    }

     

     

所有回覆

  • 2009年5月7日 上午 09:37Jo0815 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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