[RESOLVED] Memory Leak Question
Hi all, I have the following code in a class that I am using to sort large text files in C#. The code works great except that I have a memory leak. I am assuming that this is the outputArray in the SortTextFile method causing the problem. From what I can tell there is no erase like function for C#. Is there a way to make sure that outputArray is disposed of?
Thanks Steve
Code:
public string[] FileToStringArray(string filePath)
{
ArrayList fileArrayList = new ArrayList();
string inputLine;
string[] outputArray;
StreamReader sr = new StreamReader(filePath);
inputLine = sr.ReadLine();
while(inputLine != null)
{
fileArrayList.Add(inputLine);
inputLine = sr.ReadLine();
}
sr.Close();
outputArray = new string[fileArrayList.Count];
fileArrayList.CopyTo(outputArray);
return outputArray;
}
Code:
public void StringArrayToFile(string[] inputArray, string filePath)
{
StreamWriter sw = new StreamWriter(filePath);
foreach (string inputLine in inputArray)
{
sw.WriteLine(inputLine);
}
sw.Close();
}
Code:
public void SortTextFile(string inputFilePath, string outputFilePath)
{
string[] outputArray = this.FileToStringArray(inputFilePath);
Array.Sort(outputArray);
this.StringArrayToFile(outputArray, outputFilePath);
}