Skip to content

Instantly share code, notes, and snippets.

@sferrini
Last active March 31, 2016 20:50
Show Gist options
  • Select an option

  • Save sferrini/a15b853c1f1ed65dad4ef82e3d8d08ac to your computer and use it in GitHub Desktop.

Select an option

Save sferrini/a15b853c1f1ed65dad4ef82e3d8d08ac to your computer and use it in GitHub Desktop.

Revisions

  1. sferrini revised this gist Mar 31, 2016. 1 changed file with 14 additions and 11 deletions.
    25 changes: 14 additions & 11 deletions memory_leak.swift
    Original file line number Diff line number Diff line change
    @@ -1,47 +1,50 @@
    //: Playground - noun: a place where people can play

    var str = "Hello, world"
    typealias CompletionBlock = (Bool) -> ()

    class BlockStorage {

    var blocks :[CompletionBlock] = []
    var blocks = [CompletionBlock]()

    func addBlock(block: CompletionBlock) {
    blocks.append(block)
    }

    deinit {
    print("BlockStorage deallocated")
    print("BlockStorage deinit")
    }
    }

    }

    class Client {
    var blockStorage :BlockStorage = BlockStorage()

    var blockStorage = BlockStorage()

    func start() {
    blockStorage.addBlock() { [unowned self](success: Bool) in

    blockStorage.addBlock() { [unowned self](success: Bool) in
    print("\(self) First block")
    }

    // blockStorage.addBlock(doSomeThing) //if this line is uncommented the Client and the BlockStorage are not deallocated.
    blockStorage.addBlock() { [unowned self](success: Bool) in
    self.doSomeThing(success)
    }

    }

    func doSomeThing(param: Bool) {
    func doSomeThing(success: Bool) {
    print("Do Something")
    }

    deinit {
    print("Client Deallocated")
    print("Client deinit")
    }
    }

    }

    func playground() {
    let client = Client()
    client.start()
    }


    playground()
  2. @ignazioc ignazioc revised this gist Mar 31, 2016. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions memory_leak.swift
    Original file line number Diff line number Diff line change
    @@ -21,11 +21,12 @@ class Client {
    var blockStorage :BlockStorage = BlockStorage()

    func start() {
    blockStorage.addBlock() { (success: Bool) in
    print("First block")
    blockStorage.addBlock() { [unowned self](success: Bool) in

    print("\(self) First block")
    }

    blockStorage.addBlock(doSomeThing) //if this line is uncommented the Client and the BlockStorage are not deallocated.
    // blockStorage.addBlock(doSomeThing) //if this line is uncommented the Client and the BlockStorage are not deallocated.
    }

    func doSomeThing(param: Bool) {
    @@ -43,4 +44,4 @@ func playground() {
    }


    playground()
    playground()
  3. @ignazioc ignazioc created this gist Mar 31, 2016.
    46 changes: 46 additions & 0 deletions memory_leak.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    //: Playground - noun: a place where people can play

    var str = "Hello, world"
    typealias CompletionBlock = (Bool) -> ()

    class BlockStorage {

    var blocks :[CompletionBlock] = []

    func addBlock(block: CompletionBlock) {
    blocks.append(block)
    }

    deinit {
    print("BlockStorage deallocated")
    }
    }


    class Client {
    var blockStorage :BlockStorage = BlockStorage()

    func start() {
    blockStorage.addBlock() { (success: Bool) in
    print("First block")
    }

    blockStorage.addBlock(doSomeThing) //if this line is uncommented the Client and the BlockStorage are not deallocated.
    }

    func doSomeThing(param: Bool) {
    print("Do Something")
    }
    deinit {
    print("Client Deallocated")
    }
    }


    func playground() {
    let client = Client()
    client.start()
    }


    playground()