[英]Memory leak with a memory stream
我知道这段代码远非完美,但在我的情况下,这是正确执行此操作的唯一方法,因为我将 WPF 嵌入到 C# 中,并且在定期应用文本时,拼写检查无法正常工作
所以这是我的代码:
RichTextBox temphotfix = new RichTextBox();
temphotfix.Font = new Font(temphotfix.Font.Name, 14);
System.Windows.Documents.TextRange range = new System.Windows.Documents.TextRange(omschrijving.Document.ContentStart, omschrijving.Document.ContentEnd);
temphotfix.Text = oms;
string temp = temphotfix.Rtf;
byte[] byteArray = Encoding.ASCII.GetBytes(temp);
MemoryStream stream = new MemoryStream(byteArray);
range.Load(stream, DataFormats.Rtf);
range = null;
temp = null;
byteArray = null;
temphotfix.Dispose();
stream.Dispose();
我对此进行了压力测试,似乎脚本运行了大约 5 次,它增加了大约 1 MB 内存。
我做错了什么,我把我用过的一切都变成了空,或者废弃了它们。
正如我在上面的评论中所说,您可以using
,您可以尝试使用此代码。 希望这会有所帮助。
using (RichTextBox temphotfix = new RichTextBox())
{
temphotfix.Font = new Font(temphotfix.Font.Name, 14);
System.Windows.Documents.TextRange range = new System.Windows.Documents.TextRange(omschrijving.Document.ContentStart, omschrijving.Document.ContentEnd);
temphotfix.Text = oms;
string temp = temphotfix.Rtf;
byte[] byteArray = Encoding.ASCII.GetBytes(temp);
using (MemoryStream stream = new MemoryStream(byteArray))
{
range.Load(stream, DataFormats.Rtf);
}
range = null;
temp = null;
byteArray = null;
//temphotfix.Dispose();
//stream.Dispose();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.