I have this code in a network extension:
private func pathForToken(token: audit_token_t) -> String? {
var tokenCopy = token
let bufferSize = UInt32(4096)
let bytes = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(bufferSize))
let length = proc_pidpath_audittoken(&tokenCopy, bytes, bufferSize)
if length != 0 {
return String(cString: bytes).lowercased()
}
return nil
}
bytes appears to be leaked -- the call stack is pathForToken(token:) to specialized static UnsafeMutablePointer.allocate(capacity:)
Do I need to do something to ensure bytes is released, since it doesn't seem to be happening on its own?
Do I need to do something to ensure bytes is released, since it doesn't seem to be happening on its own?
Definitely! See this note in the documentation for UnsafeMutablePointer.allocate(capacity:):
When you allocate memory, always remember to deallocate once you’re finished.
The Unsafe in the name is a reminder you’re in old-school C land now, where you need to free() at some point after every malloc().