Memory leak when changing Text field of a Scintilla object.
- by PlaZmaZ
I have a relatively large program that I'm optimizing for ASCII input files around 10-80mB in size. The program reads every line of the file into a stringbuilder and then sets the Text field of the ScintillNET object to the stringbuilder. The stringbuilder is then set to null.
private void ReloadFile(string sFile)
{
txt_log.ResetText();
try
{
StringBuilder sLine = new StringBuilder("");
using (StreamReader sr = new StreamReader(sFile))
{
while (true)
{
string temp = sr.ReadLine();
if (temp == null) break;
sLine.AppendLine(temp);
}
sr.Close();
}
txt_log.Text = sLine.ToString();
sLine = null;
}
catch (Exception ex)
{
MessageBox.Show(this, "An error occurred opening this file.\n\n" + ex.Message, "File Open Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
GC.Collect();
}
The program has an option to reload or open a file. This is irrelevant, as any call to txt_log.Text seems to not get rid of the previous memory used for the .Text field. Commenting out the txt_log.Text line gives proper memory behavior. The GC.Collect() line seems pointless, and I have tried both with and without it.
Is there something I'm missing here? I HIGHLY doubt it's a problem with the ScintillaNET component itself--rather something in this code.