SaveDocument memory leak when using MemoryStream

PDF-XChange Viewer SDK for Developer's
(ActiveX and Simple DLL Versions)

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Post Reply
DSSavant
User
Posts: 216
Joined: Thu Jul 08, 2004 7:29 pm

SaveDocument memory leak when using MemoryStream

Post by DSSavant »

I have reproduced a memory leak that will quickly kill my application when using medium to large files.

Version 2.5.199
Windows 7, 64-bit
blah blah blah

I use Memory Streams in my application because I do not want anything written to disk. Many of my documents will contain VISA numbers and as such, I do not want to have little baby VISA numbers ever found on deleted parts of the disk or ...

When using Save Document and indicating that I want the file saved to a Stream, I will pass a Memory Stream instead of a File Stream. Again, I do not want to persist to disk rather I want it to go to memory. If I wanted to save the PDF to disk I would have just called SaveDocument giving it a File Path.

By changing the stream type, a memory leak will be created. The code below is from your Full Demo program with a slight change - I added a For/Next loop to show the memory leak quicker and I change the FileStream to a MemoryStream:

Code: Select all

private void b_saveStream_Click(object sender, EventArgs e)
{
    string SaveTo = "";
    if (DialogResult.OK == saveFileDialog1.ShowDialog(this))
    {
        SaveTo = saveFileDialog1.FileName;
    }
    if (SaveTo == "")
        return;

    int nFlags = 0;
    if (cb_OpenWithoutUI.Checked)
        nFlags = (int)PDFXCviewAxLib.PXCVA_Flags.PXCVA_NoUI;

    object vDataIn = "";
    object[] vArr = new object[2];

	for (int i = 0; i < 10; i++)		// ADDED
	{									// ADDED
		// FileStream writeStream = new FileStream(SaveTo, FileMode.Create);  // COMMENTED
		MemoryStream writeStream = new MemoryStream();	// ADDED

		IStreamWrapper istr = new IStreamWrapper(writeStream);
		object varg1 = g_nActiveDocID;
		object varg2 = istr;
		vArr[0] = varg1;
		vArr[1] = varg2;
		vDataIn = vArr;


		object OutPar;
		try
		{
			axCoPDFXCview1.DoVerb(null, "SaveDocument", vDataIn, out OutPar, nFlags);
		}
		catch (Exception ex)
		{
			ShowErrorMessage(System.Runtime.InteropServices.Marshal.GetHRForException(ex));
		}

		writeStream.Close();
	}	// ADDED
}
To reproduce this, simply load a 10 or 20 Meg PDF and then tell it to SaveStream on the Document tab. Your memory footprint will grow and never ever come down to a reasonable number.

So, the question is this, I can't use File Streams because I need to keep the file off the disk, how do I do this?

Thanks.
User avatar
Vasyl-Tracker Dev Team
Site Admin
Posts: 2353
Joined: Thu Jun 30, 2005 4:11 pm
Location: Canada

Re: SaveDocument memory leak when using MemoryStream

Post by Vasyl-Tracker Dev Team »

We found reason of it. The IStreamWrapper holds your MemoryStream for too long (thank the NET's garbage collector for this :( )
Solution for this problem:
1. add new simple function to IStreamWrapper implementation:

Code: Select all

public class IStreamWrapper : IStream
{
++++
	public void Close()
	{
		this.m_stream = null;
	};
++++
};
2. Call it before closing of your MemoryStream:

Code: Select all

	...
	istr.Close(); // IStreamWrapper
	writeStream.Close(); // MemoryStream
	...
HTH
Vasyl Yaremyn
Tracker Software Products
Project Developer

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
DSSavant
User
Posts: 216
Joined: Thu Jul 08, 2004 7:29 pm

Re: SaveDocument memory leak when using MemoryStream

Post by DSSavant »

Great, thanks.
User avatar
John - Tracker Supp
Site Admin
Posts: 5219
Joined: Tue Jun 29, 2004 10:34 am
Location: United Kingdom
Contact:

Re: SaveDocument memory leak when using MemoryStream

Post by John - Tracker Supp »

Pleasure :)
If posting files to this forum - you must archive the files to a ZIP, RAR or 7z file or they will not be uploaded - thank you.

Best regards
Tracker Support
http://www.tracker-software.com
Post Reply