2

I have made a ViewController for testing with code below

class SomeClass {

    init() {
        println("Some entity created")
    }

    deinit {
        println("Some entity released")
    }

}


class ViewController: UIViewController {

    var dict = [String: SomeClass]()

    @IBAction func buttonTouch(sender: UIButton) {
        var some = SomeClass()

        dict["1"] = some
        dict.removeValueForKey("1")
        dict = [String: SomeClass]()
    }

}

The problem is that the entity (some) stored in property dict will never receive deinit call. I see only "Some entity created". Is it right behaviour?

EDITED:

It has been fixed in new Swift version

kostyl
  • 339
  • 3
  • 15
  • Do you ever remove that key from the dictionary? or remove the view controller (and thus the dictionary)? The dictionary is going to keep a reference to that object as long as the key is in it, and the view controller is going to keep a reference to the dictionary as long as it is around. – Nicholas Hart Jul 15 '14 at 18:13
  • are you testing this in an app, the REPL, or a playground? – drewag Jul 15 '14 at 18:15
  • 1
    I think it is a bug in current Swift implementation. Similar problem was mentioned here http://stackoverflow.com/questions/24700178/retain-cycle-when-grabing-values-or-keys-from-dictionary-in-swift – Keenle Jul 15 '14 at 18:25
  • I am testing this in app. This entity is alive after the UIViewController dies – kostyl Jul 15 '14 at 18:34
  • try `dict["1"] = nil` – user2727195 Jan 07 '15 at 15:00
  • your example is working fine, I'm seeing both entity created and released messages – user2727195 Jan 07 '15 at 15:05
  • @user2727195 it was in older version – kostyl Jan 08 '15 at 19:35

0 Answers0