VIDEODEV
VIDEODEV

Reputation: 23

Memory Leaks using C# DLL in VC++ Project

We have created a DLL in C# to read an RTF File using System.Windows.Forms.RichTextBox. After receiving an RTF File name it returns us the just Text from file without attributes.

Code is Provided as below.

namespace RTF2TXTConverter
{
    public interface IRTF2TXTConverter
    {
         string Convert(string strRTFTxt);

    };

    public class RTf2TXT : IRTF2TXTConverter
    {
        public string Convert(string strRTFTxt)
        {
            string path = strRTFTxt;

            //Create the RichTextBox. (Requires a reference to System.Windows.Forms.)
            System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();

            // Get the contents of the RTF file. When the contents of the file are   
            // stored in the string (rtfText), the contents are encoded as UTF-16.  
            string rtfText = System.IO.File.ReadAllText(path);

            // Display the RTF text. This should look like the contents of your file.
            //System.Windows.Forms.MessageBox.Show(rtfText);

            // Use the RichTextBox to convert the RTF code to plain text.
            rtBox.Rtf = rtfText;
            string plainText = rtBox.Text;

            //System.Windows.Forms.MessageBox.Show(plainText);

            // Output the plain text to a file, encoded as UTF-8. 
            //System.IO.File.WriteAllText(@"output.txt", plainText);

            return plainText;
        }
    }
}

Convert method returns plaintext from RTF file. In VC++ application each time we load an RTF file memory usage goes on

increasing. After each iteration memory usage is increased by 1 MB.

Do we need to unload the DLL after usage and load again for new RTF file? Does RichTextBox control stay in memory always? or any other reason....

We have tried a lot but we can't find any solution.Any help in this regard will be great help.

Upvotes: 2

Views: 339

Answers (1)

Scott Chamberlain
Scott Chamberlain

Reputation: 127603

I ran in to a similar issue, the constant creation of Rich Text Box controls can cause some issues. Another big issue you will run in to if you are doing this method repeatedly on a large set of data is that it takes a long time for your rtBox.Rtf = rtfText; line to complete the very first time the control is used (there is some lazy loading that happens in the background that does not happen till the first time you set the text).

The work around for this is re-use the control for subsequent calls, by doing this you only have to pay the costly initialization cost once. Here is a copy of the code I used, I was working in a multi-threaded environment so we actually used a ThreadLocal<RichTextBox> to old the control.

//reuse the same rtfBox object over instead of creating/disposing a new one each time ToPlainText is called
static ThreadLocal<RichTextBox> rtfBox = new ThreadLocal<RichTextBox>(() => new RichTextBox());

public static string ToPlainText(this string sourceString)
{
    if (sourceString == null)
        return null;

    rtfBox.Value.Rtf = sourceString;
    var strippedText = rtfBox.Value.Text;
    rtfBox.Value.Clear();

    return strippedText;
}

See if re-using a thread local Rich Text Box will stop the constant 1MB growth per call.

Upvotes: 1

Related Questions