I’ve got a significant memory leak in this little bit of code that parses out a single line from a CSV file:
Public Function ParseCSVRow(row As String) As String()
// parses a single row of CSV data
// properly handles embedded commas, but nothing more complex than that
Var flds() As String
var fld As String
Var i As Integer
Var fieldStart As Integer = 0
Var insideQuotes As Boolean = False
Var currentChar As String
For i = 0 To row.Length-1
currentChar = row.Middle(i, 1)
If currentChar = """" Then
insideQuotes = Not insideQuotes
ElseIf currentChar = "," And Not insideQuotes Then
flds.Add(row.Middle(fieldStart, i - fieldStart).Trim)
fieldStart = i + 1
End
Next
flds.Add(row.Middle(fieldStart).Trim) // catch the last field
// flds = row.ToArray(",")
For i = 0 To flds.LastIndex
fld = flds(i)
If fld.Left(1) = """" And fld.Right(1) = """" Then
flds(i) = fld.Middle(1, fld.Len - 2)
End
Next
Return flds
End Function
The CSV file is about 5 MB, with about 32,000 lines of 8 fields. I don’t hold onto any of the data, it gets inserted directly into a SQLite db, but the app’s memory usage will go from 45MB to as high as 35GB.
i have replaced the above code with a simple row.ToArray(“,”) and there are no leaks, but the data has embedded commas so I can’t use ToArray().
I have also tried the Xcode Instruments app to narrow things down a little more, but haven’t been able to get it to record - it gives a “Required kernel recording resources are in use by another document.” error.
Does anyone have any ideas here? I’m stuck…