将C#dll用于MFC代码时的内存泄漏

时间:2013-12-05 11:01:09

标签: c# c++

我想创建解析器来读取RTF文件的文本。 所以我使用RichTextBox在C#中创建了一个dll。 之后我将它从dll转换为tlb。 并从cpp。

调用它

但它会在循环中产生内存泄漏,应用程序内存不断增加。

我附加了两个代码snipet ..

请帮帮我。

谢谢

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;

        rtBox.Clear();
        rtBox.Dispose();
        //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;
    }

Cpp代码我正在从dll中读取文本。

    long lResult =0;
std::string str ;

char* tcFileName = new char[m_strFilePath.GetLength()+1]; 

USES_CONVERSION;

strcpy (tcFileName, T2A (m_strFilePath));


printf("char * text: %s\n", tcFileName);


BSTR bstrText = _com_util::ConvertStringToBSTR(tcFileName);
BSTR  bstrReturnValue;
wprintf(L"BSTR text: %s\n", bstrText);

iRTFConverter->Convert(bstrText,&bstrReturnValue);
//iRTFConverter->
CString strCIFContent(bstrReturnValue);

ParseString(strCIFContent);

delete []tcFileName;
tcFileName = NULL;

iRTFConverter->Release();
SysFreeString(bstrText);
bstrText = NULL;
SysFreeString(bstrReturnValue);

0 个答案:

没有答案