[swift-users] Using #function causes big memory leak

Edward Connell ewconnell at gmail.com
Sun Aug 27 12:56:18 CDT 2017


I reported this about a year ago, but it has never been fixed and it seems
like it should be fixed for the Swift 4.0 release.

Here is a simple repro case. If you watch the memory monitor as it runs,
you see memory consumption climb to 2.7GB when using #function, and no
memory increase when using a static string.

import Foundation

class A {
var counter = 0 {
// didSet { onSet("counter") }  // no leak
didSet { onSet() }  // huge leak
}

var properties = ["counter" : 0]
func onSet(_ name: String = #function) {
properties[name]! += 1
}
}

var myclass = A()

for i in 0..<10000000 {
myclass.counter = i
}

print(myclass.properties["counter"]!)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170827/c3532e34/attachment.html>


More information about the swift-users mailing list