Created
June 3, 2014 20:53
-
-
Save venkatperi/d7c45b1c0c7bbeeff260 to your computer and use it in GitHub Desktop.
Memory Leaks in Swift Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
var activeObjects = Int[]() | |
var id = 1000 | |
var indentLevel = 0 | |
func indent(){ | |
indentLevel+=2 | |
} | |
func outdent(){ | |
indentLevel -= 2 | |
} | |
class Info { | |
var name: String | |
var uuid = ++id | |
init(name: String){ | |
self.name = name | |
log("\(self.name)/init()") | |
activeObjects += uuid | |
} | |
deinit{ | |
log("\(self.name)/deinit()") | |
if let idx = find(activeObjects, self.uuid) { | |
activeObjects.removeAtIndex(idx) | |
} | |
else { | |
log("odd: I wasn't in the active objects list!") | |
} | |
} | |
func log(msg: String){ | |
var ind = "" | |
for i in 0..indentLevel { | |
ind += "." | |
} | |
println("\(uuid) -- \(ind)\(msg)") | |
} | |
} | |
class X : Info { | |
init(prefix : String){ | |
super.init(name: "\(prefix):X") | |
} | |
} | |
class Test : Info{ | |
init(prefix: String){ | |
super.init(name: "\(prefix):Test") | |
} | |
func test1() { | |
log("\(name)/test1()") | |
indent() | |
var x : X? | |
x=X(prefix: "\(name)/test1") | |
x=nil | |
test2(X(prefix: name)) | |
outdent() | |
} | |
func test2(x: X){ | |
log("\(name)/test2()") | |
} | |
func test3(){ | |
log("\(name)/test3()") | |
indent() | |
var t = Test(prefix: "\(name)/test3") | |
t.test1() | |
outdent() | |
} | |
func test4(t: Test){ | |
log("\(name)/test4()") | |
indent() | |
t.test1() | |
t.test2(X(prefix: "\(name)/test4")) | |
t.test3() | |
outdent() | |
} | |
} | |
var prefix = "" | |
var t : Test? | |
t = Test(prefix: prefix) | |
t!.test1() | |
println("") | |
t!.test2(X(prefix: prefix)) | |
println("") | |
t!.test3() | |
println("") | |
t!.test4(Test(prefix: prefix)) | |
t = nil | |
println("\nactive objects: ") | |
for item in activeObjects { | |
println (item) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1001 -- :Test/init() | |
1001 -- :Test/test1() | |
1002 -- ..:Test/test1:X/init() | |
1003 -- ..:Test:X/init() | |
1001 -- ..:Test/test2() | |
1003 -- ..:Test:X/deinit() | |
1004 -- :X/init() | |
1001 -- :Test/test2() | |
1004 -- :X/deinit() | |
1001 -- :Test/test3() | |
1005 -- ..:Test/test3:Test/init() | |
1005 -- ..:Test/test3:Test/test1() | |
1006 -- ....:Test/test3:Test/test1:X/init() | |
1007 -- ....:Test/test3:Test:X/init() | |
1005 -- ....:Test/test3:Test/test2() | |
1007 -- ....:Test/test3:Test:X/deinit() | |
1008 -- :Test/init() | |
1001 -- :Test/test4() | |
1008 -- ..:Test/test1() | |
1009 -- ....:Test/test1:X/init() | |
1010 -- ....:Test:X/init() | |
1008 -- ....:Test/test2() | |
1010 -- ....:Test:X/deinit() | |
1011 -- ..:Test/test4:X/init() | |
1008 -- ..:Test/test2() | |
1011 -- ..:Test/test4:X/deinit() | |
1008 -- ..:Test/test3() | |
1012 -- ....:Test/test3:Test/init() | |
1012 -- ....:Test/test3:Test/test1() | |
1013 -- ......:Test/test3:Test/test1:X/init() | |
1014 -- ......:Test/test3:Test:X/init() | |
1012 -- ......:Test/test3:Test/test2() | |
1014 -- ......:Test/test3:Test:X/deinit() | |
active objects: | |
1001 | |
1002 | |
1005 | |
1006 | |
1008 | |
1009 | |
1012 | |
1013 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment