crossroadlabs / Markdown

Swift markdown library
GNU General Public License v3.0
80 stars 10 forks source link

Memory leak #9

Open SiriusBrent opened 7 years ago

SiriusBrent commented 7 years ago

There's a memory leak in Markdown.swift at line 47:

var dest = [UnsafeMutablePointer<Int8>?](repeating: UnsafeMutablePointer<Int8>.allocate(capacity: 1), count: 1)

This line is allocating space for a pointer to be returned, but in doing so is also allocating an actual pointer to one byte of memory. When fun is called to process data and return a result, the allocated pointer is overwritten and the one-byte allocation (16 actual bytes) is lost.

Since all you need is space for a pointer here, and since the array is an array-of-optionals, you can just replace that line with this:

var dest: [UnsafeMutablePointer<Int8>?] = [nil]

No allocation, so no leak.