none
Memory Leak while opening the XPS document in DocumentViewer RRS feed

  • Question

  •  

    FilePath is XPS file name

    xpsDoc = new XpsDocument(FilePath, FileAccess.Read, CompressionOption.NotCompressed);


    FixedDocumentSequence fixedDocumentSequence = xpsDoc.GetFixedDocumentSequence();                
    //I am getting an memory leak in the above line of code

    DocViewer.Document = fixedDocumentSequence as IDocumentPaginatorSource;

          
    Tuesday, November 5, 2013 7:48 PM

Answers

  • Hello,

    It is strange for that. Because I made a sample with the codes. However it worked fine.

    My test codes is like below:

    internal void Execute()
    
            {
    
                string xpsFilename = "E:\\BMX\\DownLoad\\OPFile\\2013-11\\sample.xps";
    
                // Save a reference to the currently open XPS package.
    
                XpsDocument _xpsPackage = new XpsDocument(xpsFilename, FileAccess.Read, CompressionOption.NotCompressed);
    
    
                // Open the package for the new XPS document.
    
    
    
                // Get the FixedDocumentSequence from the package.
    
                FixedDocumentSequence fixedDocumentSequence = _xpsPackage.GetFixedDocumentSequence();
    
    
                // Set the new FixedDocumentSequence as 
    
                // the DocumentViewer's paginator source.
    
                DocumentViewer docViewer = new DocumentViewer();
    
                docViewer.Document = fixedDocumentSequence as IDocumentPaginatorSource;
    
    
                // If there was an old XPS package, close it now that 
    
                // DocumentViewer no longer needs to access it. 
    
    
                // Leave the new _xpsPackage open for DocumentViewer 
    
                // to access additional required resources.
    
            }
    

    And for memory leak, there are a lot of feature which can cause it.

    For solve this, please have a look at links below.

    How to Find A Memory Leak: http://msdn.microsoft.com/en-us/library/ms859415.aspx

    Memory Leak Detection and Isolation: http://msdn.microsoft.com/en-us/library/x98tx3cf(v=vs.90).aspx

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 7, 2013 7:41 AM
    Moderator

All replies

  • Hello,

    It is strange for that. Because I made a sample with the codes. However it worked fine.

    My test codes is like below:

    internal void Execute()
    
            {
    
                string xpsFilename = "E:\\BMX\\DownLoad\\OPFile\\2013-11\\sample.xps";
    
                // Save a reference to the currently open XPS package.
    
                XpsDocument _xpsPackage = new XpsDocument(xpsFilename, FileAccess.Read, CompressionOption.NotCompressed);
    
    
                // Open the package for the new XPS document.
    
    
    
                // Get the FixedDocumentSequence from the package.
    
                FixedDocumentSequence fixedDocumentSequence = _xpsPackage.GetFixedDocumentSequence();
    
    
                // Set the new FixedDocumentSequence as 
    
                // the DocumentViewer's paginator source.
    
                DocumentViewer docViewer = new DocumentViewer();
    
                docViewer.Document = fixedDocumentSequence as IDocumentPaginatorSource;
    
    
                // If there was an old XPS package, close it now that 
    
                // DocumentViewer no longer needs to access it. 
    
    
                // Leave the new _xpsPackage open for DocumentViewer 
    
                // to access additional required resources.
    
            }
    

    And for memory leak, there are a lot of feature which can cause it.

    For solve this, please have a look at links below.

    How to Find A Memory Leak: http://msdn.microsoft.com/en-us/library/ms859415.aspx

    Memory Leak Detection and Isolation: http://msdn.microsoft.com/en-us/library/x98tx3cf(v=vs.90).aspx

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 7, 2013 7:41 AM
    Moderator
  • I apologize , my description of the problme was not correct.

    Actually XPS document i am generating is very large(>2000 pages)

    So all the XPS pages are first generated in temp location and then i will merge to fixed document sequence

    Below method will be called for each of the XPS file.After iterating for few files it will throw a exception which will nothing but related to Memory Shortage

    Complete Code :                             

    /// <summary>
            /// Merges the given xps file (source) into the given fixed document sequence (destination)
            /// </summary>
            /// <param name="xpsFilePath">The source file path</param>
            /// <param name="fdSeqNew">The destination fixed document sequence reference</param>
            public static void MergeXpsDocs(string xpsFilePath, FixedDocumentSequence fdSeqNew)
            {
                using (XpsDocument xpsDocOld = new XpsDocument(xpsFilePath, FileAccess.Read))
                {
    FixedDocumentSequence fdSeqOld = xpsDocOld.GetFixedDocumentSequence();
                    System.Windows.Documents.DocumentPaginator DocPager = fdSeqOld.DocumentPaginator;
                    DocPager.ComputePageCount();
                    //For PageIndex As Integer = 0 To DocPager.PageCount - 1

                    for (int PageIndex = 0; PageIndex < DocPager.PageCount-1;PageIndex++)
                    {
                         (DocPager.GetPage(PageIndex).Visual as System.Windows.Documents.FixedPage).UpdateLayout();                                        
                    }

                    foreach (DocumentReference refOld in fdSeqOld.References)
                    {
                        DocumentReference newRef = new DocumentReference();
                        (newRef as IUriContext).BaseUri = (refOld as IUriContext).BaseUri;
                        newRef.Source = refOld.Source;
                        fdSeqNew.References.Add(newRef);
                    }
                    xpsDocOld.Close();
                }
            }

    Tuesday, December 3, 2013 9:29 AM
  • I apologize , my description of the problme was not correct.

    Actually XPS document i am generating is very large(>2000 pages)

    So all the XPS pages are first generated in temp location and then i will merge to fixed document sequence

    Below method will be called for each of the XPS file.After iterating for few files it will throw a exception which will nothing but related to Memory Shortage

    Complete Code :                             

    /// <summary>
            /// Merges the given xps file (source) into the given fixed document sequence (destination)
            /// </summary>
            /// <param name="xpsFilePath">The source file path</param>
            /// <param name="fdSeqNew">The destination fixed document sequence reference</param>
            public static void MergeXpsDocs(string xpsFilePath, FixedDocumentSequence fdSeqNew)
            {
                using (XpsDocument xpsDocOld = new XpsDocument(xpsFilePath, FileAccess.Read))
                {
    FixedDocumentSequence fdSeqOld = xpsDocOld.GetFixedDocumentSequence();
                    System.Windows.Documents.DocumentPaginator DocPager = fdSeqOld.DocumentPaginator;
                    DocPager.ComputePageCount();
                    //For PageIndex As Integer = 0 To DocPager.PageCount - 1

                    for (int PageIndex = 0; PageIndex < DocPager.PageCount-1;PageIndex++)
                    {
                         (DocPager.GetPage(PageIndex).Visual as System.Windows.Documents.FixedPage).UpdateLayout();                                        
                    }

                    foreach (DocumentReference refOld in fdSeqOld.References)
                    {
                        DocumentReference newRef = new DocumentReference();
                        (newRef as IUriContext).BaseUri = (refOld as IUriContext).BaseUri;
                        newRef.Source = refOld.Source;
                        fdSeqNew.References.Add(newRef);
                    }
                    xpsDocOld.Close();
                }
            }


    Friday, January 10, 2014 11:46 AM