When to use weak references in Python?

Events are a common scenario for weak references. Problem Consider a pair of objects: Emitter and Receiver. The receiver has shorter lifetime than the emitter. You could try an implementation like this: class Emitter(object): def __init__(self): self.listeners = set() def emit(self): for listener in self.listeners: # Notify listener(‘hello’) class Receiver(object): def __init__(self, emitter): emitter.listeners.add(self.callback) def … Read more

Collections of zeroing weak references under ARC

Zeroing weak references require OS X 10.7 or iOS 5. You can only define weak variables in code, ivars or blocks. AFAIK there is no way to dynamically (at runtime) to create a weak variable because ARC takes effect during compile time. When you run the code it already has the retains and releases added … Read more

How can you capture multiple arguments weakly in a Swift closure?

From Expressions in “The Swift Programming Language” (emphasis added): Closure Expression … A closure expression can explicitly specify the values that it captures from the surrounding scope using a capture list. A capture list is written as a comma separated list surrounded by square brackets, before the list of parameters. If you use a capture … Read more

Is Josh Smith’s implementation of the RelayCommand flawed?

I’ve found the answer in Josh’s comment on his “Understanding Routed Commands” article: […] you have to use the WeakEvent pattern in your CanExecuteChanged event. This is because visual elements will hook that event, and since the command object might never be garbage collected until the app shuts down, there is a very real potential … Read more

Strong and weak references in Swift

Straight from the Swift Language guide: class Person { let name: String init(name: String) { self.name = name } var apartment: Apartment? deinit { println(“\(name) is being deinitialized”) } } class Apartment { let number: Int init(number: Int) { self.number = number } weak var tenant: Person? deinit { println(“Apartment #\(number) is being deinitialized”) } … Read more

WeakReference/AsyncTask pattern in android

How does this resolve the situation ? The WeakReference allows the Activity to be garbage collected, so you don’t have a memory leak. A null reference means that the AsyncTask cannot blindly try to update a user-interface that is no longer attached, which would throw exceptions (e.g. view not attached to window manager). Of course … Read more

Is there a SoftHashMap in Java?

Edit (Aug. 2012): It turns out that currently the best solution are probably Guava 13.0’s Cache classes, explained on Guava’s Wiki – that’s what I’m going to use. It even supports building a SoftHashMap (see CacheBuilder.newBuilder().softKeys()), but it is probably not what you want, as Java expert Jeremy Manson explains (below you’ll find the link). … Read more