在MFC代码中使用C#dll时内存泄漏

在MFC代码中使用C#dll时内存泄漏,c#,c++,C#,C++,我想创建用于读取RTF文件文本的解析器。 因此,我使用RichTextBox在C#中创建了一个dll。 之后,我将其从dll转换为tlb。 从cpp那里打电话 但它在循环中产生内存泄漏,应用程序内存不断增加 我附上两个代码snipet 请帮帮我 多谢各位 public string Convert(string strRTFTxt) { string path = strRTFTxt; //Create the RichTextBox. (Require

我想创建用于读取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;
    }
我从dll读取文本的Cpp代码

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

ParseString
中发生了什么?众所周知,人们对C#中使用的垃圾收集器的行为知之甚少。在互操作代码中使用的一个标准问题是,没有创建足够的垃圾来诱使它完成其工作。另一个是使用糟糕的内存分析器,任务管理器在显示实际情况方面尤其糟糕。通过测试这段代码,找出您是否真的有漏洞,或者只是误解了内存管理。只需运行一百万次。如果测试炸弹带有OOM,那么你知道你真的有漏洞。Thorsten Dittmar ParseString只是逐行提取文本并将其推入堆栈。