Get started with App.net.

  Sign up now  
cparnot
    NSMapTable WTF! “weak-to-strong NSMapTables are not currently recommended as the strong values for weak keys which get zero'd out do not get cleared away” — https://developer.apple.com/library/mac/releasenotes/Foundation/RN-FoundationOlderNotes/
    There are 12 new posts
    cparnot
      @mjtsai I rewrote using 2 tables, a weak-to-weak + strong-to-weak, but from my experimentation, it seems strong-to-weak has the same limitation. Also, the `count` property is different from key/objectEnumerator.allObjects.count when content is zeroed.
      p2
        @cparnot Hard to imagine another behavior for weak-to-strong, or am I missing something?
        cparnot
          @p2 If the key is dealloc-ed, the entry is gone (objectForKey: returns nil) but the object is not released (until the table is eventually resized).
          p2
            @cparnot Yes, doesn’t this make sense? Or would you prefer to release the object when the key disappears? IMHO that wouldn’t exactly qualify as “strong”, but I can see use for this.
            mjtsai
              @cparnot What do you mean by "the same limitation" for a strong-to-weak NSMapTable? Surely -removeObjectForKey: removes the hash bucket…
              mjtsai
                @p2 @cparnot Yes, removing the weak key should remove the strong reference to the object. Otherwise, your map will leak. Strong refers to the fact that the map retains the object for as long as it contains the (valid) key.
                cparnot
                  @mjtsai (1) Yes, `removeObjectForKey:` properly releases the key/object in both weak-to-strong, or strong-to-weak. But you have to do it before zeroing happens…
                  cparnot
                    @mjtsai (2) Zeroing the (weak) object or key, does not immediately release the (strong) key or object, and the entry is *gone* and can’t be reached anymore via the public APIs (not sure about `removeForKey:` actually).
                    cparnot
                      @mjtsai (3) Needs more investigation… And sorry for the long split-up response ;-)
                      cparnot
                        @p2 What Michael said :-)
                        mjtsai
                          @cparnot I would expect zeroing the weak object to make the entry gone. Are you saying this leaks if zeroing happens before you -removeObjectToKey:?
                          cparnot
                            @mjtsai Yes, it leaks (until NSMapTable is reallocated or until it purges those entries, e.g. after resizing or some other unknown internal event; if you don’t touch the NSMapTable and keep it alive, it stays as-is forever).
                            cparnot
                              @mjtsai Amusingly `removeObjectForKey:` does release the (strong) key even if called after the object value has been zeroed out.
                              mjtsai
                                @cparnot The documentation is not very clear. I would expect it to retain the key until you remove it but for that entry not to show up in the enumerator or count.
                                cparnot
                                  @mjtsai You can play with this test app and see for yourself: https://github.com/cparnot/KitchenSink/tree/master/NSMapTable+Zeroing — I’ll post about it later.
                                  mjtsai
                                    @cparnot The strong key should not be released when the table resizes, IMO.
                                    cparnot
                                      @mjtsai Ah, I see the logic, though I would expect to behave more like the weak-to-strong for symmetry… In any case, it does eventually “purge” the keys, from what I see.
                                      cparnot
                                        [Post deleted]
                                        cparnot
                                          @mjtsai And to be clear, when the object is zeroed, `objectForKey:` does return nil for that key. It’s just that the key is retained for a while longer. And `count` returns a useless value.
                                          p2
                                            @cparnot nice writeup, thanks for reminding me.