Skip to content

Instantly share code, notes, and snippets.

@ezura
Created April 22, 2017 02:44
Show Gist options
  • Save ezura/2b3b13ea5978b6266c809b57aceff1ac to your computer and use it in GitHub Desktop.
Save ezura/2b3b13ea5978b6266c809b57aceff1ac to your computer and use it in GitHub Desktop.
retain count 調べ
class Obj2 {
var tag: String
init(_ tag: String) {
self.tag = tag
}
func defer1() {
print(tag, #function, "start", CFGetRetainCount(self))
defer { print(tag, #function, "defer", CFGetRetainCount(self)) }
print(tag, #function, "end", CFGetRetainCount(self))
}
func defer2() {
print(tag, #function, "start", CFGetRetainCount(self))
defer { print(tag, #function, "defer", CFGetRetainCount(self)) }
defer { print(tag, #function, "defer", CFGetRetainCount(self)) }
print(tag, #function, "end", CFGetRetainCount(self))
}
func deferNest() {
print(tag, #function, "start", CFGetRetainCount(self))
defer { [tag]
print(tag, #function, "defer layer1", CFGetRetainCount(self))
defer {
print(tag, #function, "defer layer2", CFGetRetainCount(self))
}
print(tag, #function, "defer layer1", CFGetRetainCount(self))
}
print(tag, #function, "end", CFGetRetainCount(self))
}
func deferTagChange() {
print(tag, #function, "start", CFGetRetainCount(self))
defer { [tag]
print(tag, #function, "defer", CFGetRetainCount(self))
}
tag = "change"
print(tag, #function, "end", CFGetRetainCount(self))
}
func noescapeClosure() {
print(tag, #function, "start", CFGetRetainCount(self))
let closure: @noescape () -> Void = {
print(tag, #function, "noescape closure", CFGetRetainCount(self))
}
closure()
print(tag, #function, "end", CFGetRetainCount(self))
closure()
}
func escapeClosure() {
print(tag, #function, "start", CFGetRetainCount(self))
let closure: () -> Void = {
print(self.tag, #function, "noescape closure", CFGetRetainCount(self))
}
closure()
print(tag, #function, "end", CFGetRetainCount(self))
closure()
}
deinit {
print(tag, "deinit", CFGetRetainCount(self))
defer {
print(tag, #function, "defer", CFGetRetainCount(self))
}
}
}
do {
var obj = Obj2("first")
print("--------------------------")
obj.defer1()
print("--------------------------")
obj.defer2()
print("--------------------------")
obj.deferNest()
print("--------------------------")
obj.deferTagChange()
print("--------------------------")
obj.noescapeClosure()
print("--------------------------")
obj.escapeClosure()
print("--------------------------")
obj = Obj2("dummy")
}
/*
--------------------------
first defer1() start 3
first defer1() end 3
first defer1() defer 4
--------------------------
first defer2() start 3
first defer2() end 3
first defer2() defer 4
first defer2() defer 4
--------------------------
first deferNest() start 3
first deferNest() end 3
first deferNest() defer layer1 4
first deferNest() defer layer1 4
first deferNest() defer layer2 5
--------------------------
first deferTagChange() start 3
change deferTagChange() end 3
change deferTagChange() defer 4
--------------------------
change noescapeClosure() start 3
change noescapeClosure() noescape closure 5
change noescapeClosure() end 4
change noescapeClosure() noescape closure 5
--------------------------
change escapeClosure() start 3
change escapeClosure() noescape closure 5
change escapeClosure() end 4
change escapeClosure() noescape closure 5
--------------------------
change deinit 1
change deinit defer 2
dummy deinit 1
dummy deinit defer 2
*/
//: [Previous](@previous)
import Foundation
protocol RetainCountQuickLookable: CustomPlaygroundQuickLookable {}
extension RetainCountQuickLookable where Self: CFTypeRef {
var customPlaygroundQuickLook: PlaygroundQuickLook {
return .text("retain count: \(CFGetRetainCount(self))")
}
}
class Obj: RetainCountQuickLookable {
func function() {
print(#function, "start", CFGetRetainCount(self))
defer { print(#function, "defer", CFGetRetainCount(self)) }
sleep(2)
print(#function, "end", CFGetRetainCount(self))
}
deinit {
print("deinit", CFGetRetainCount(self))
}
}
do {
var obj = Obj() as Optional
print("create obj", CFGetRetainCount(obj))
DispatchQueue.global().async { [weak obj] in
print("before call", CFGetRetainCount(obj))
obj?.function()
}
print("after dispatch", CFGetRetainCount(obj))
obj = nil
print("obj = nil")
}
//sleep(5)
print("end")
print("---------------------------------")
class Obj2: RetainCountQuickLookable {
var tag: String
init(_ tag: String) {
self.tag = tag
}
func defer1() {
print(tag, #function, "start", CFGetRetainCount(self))
defer { print(tag, #function, "defer", CFGetRetainCount(self)) }
print(tag, #function, "end", CFGetRetainCount(self))
}
func defer2() {
print(tag, #function, "start", CFGetRetainCount(self))
defer { print(tag, #function, "defer", CFGetRetainCount(self)) }
defer { print(tag, #function, "defer", CFGetRetainCount(self)) }
print(tag, #function, "end", CFGetRetainCount(self))
}
func deferNest() {
print(tag, #function, "start", CFGetRetainCount(self))
defer { [tag]
print(tag, #function, "defer layer1", CFGetRetainCount(self))
defer {
print(tag, #function, "defer layer2", CFGetRetainCount(self))
}
print(tag, #function, "defer layer1", CFGetRetainCount(self))
}
print(tag, #function, "end", CFGetRetainCount(self))
}
func deferTagChange() {
print(tag, #function, "start", CFGetRetainCount(self))
defer { [tag]
print(tag, #function, "defer", CFGetRetainCount(self))
}
tag = "change"
print(tag, #function, "end", CFGetRetainCount(self))
}
func noescapeClosure() {
print(tag, #function, "start", CFGetRetainCount(self))
let closure: @noescape () -> Void = {
print(tag, #function, "noescape closure", CFGetRetainCount(self))
}
closure()
print(tag, #function, "end", CFGetRetainCount(self))
closure()
}
func escapeClosure() {
print(tag, #function, "start", CFGetRetainCount(self))
let closure: () -> Void = {
print(self.tag, #function, "noescape closure", CFGetRetainCount(self))
}
closure()
print(tag, #function, "end", CFGetRetainCount(self))
closure()
}
deinit {
print(tag, "deinit", CFGetRetainCount(self))
defer {
print(tag, #function, "defer", CFGetRetainCount(self))
}
}
}
do {
var obj = Obj2("first")
print("--------------------------")
obj.defer1()
print("--------------------------")
obj.defer2()
print("--------------------------")
obj.deferNest()
print("--------------------------")
obj.deferTagChange()
print("--------------------------")
obj.noescapeClosure()
print("--------------------------")
obj.escapeClosure()
print("--------------------------")
obj = Obj2("dummy")
}
/*
--------------------------
first defer1() start 3
first defer1() end 3
first defer1() defer 4
--------------------------
first defer2() start 3
first defer2() end 3
first defer2() defer 4
first defer2() defer 4
--------------------------
first deferNest() start 3
first deferNest() end 3
first deferNest() defer layer1 4
first deferNest() defer layer1 4
first deferNest() defer layer2 5
--------------------------
first deferTagChange() start 3
change deferTagChange() end 3
change deferTagChange() defer 4
--------------------------
change noescapeClosure() start 3
change noescapeClosure() noescape closure 5
change noescapeClosure() end 4
change noescapeClosure() noescape closure 5
--------------------------
change escapeClosure() start 3
change escapeClosure() noescape closure 5
change escapeClosure() end 4
change escapeClosure() noescape closure 5
--------------------------
change deinit 1
change deinit defer 2
dummy deinit 1
dummy deinit defer 2
*/
//: [Next](@next)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment