Do capture lists of inner closures need to redeclare `self` as `weak` or `unowned`?

The [weak self] in anotherFunctionWithTrailingClosure is not needed. You can empirically test this: class Experiment { func someFunctionWithTrailingClosure(closure: @escaping () -> Void) { print(“starting”, #function) DispatchQueue.main.asyncAfter(deadline: .now() + 1) { closure() print(“finishing”, #function) } } func anotherFunctionWithTrailingClosure(closure: @escaping () -> Void) { print(“starting”, #function) DispatchQueue.main.asyncAfter(deadline: .now() + 1) { closure() print(“finishing”, #function) } } func … Read more

What is the difference between a __weak and a __block reference?

From the docs about __block __block variables live in storage that is shared between the lexical scope of the variable and all blocks and block copies declared or created within the variable’s lexical scope. Thus, the storage will survive the destruction of the stack frame if any copies of the blocks declared within the frame … Read more

Garbage Collection should have removed object but WeakReference.IsAlive still returning true

Hit the same issue as you – my test was passing everywhere, except for under NCrunch (could be any other instrumentation in your case). Hm. Debugging with SOS revealed additional roots held on a call stack of a test method. My guess is that they were a result of code instrumentation that disabled any compiler … Read more

Weak reference benefits

Soft and phantom references come from Java, I believe. A long weak reference (pass true to C#’s WeakReference constructor) might be considered similar to Java’s PhantomReference. If there is an analog to SoftReference in C#, I don’t know what it is. Weak references do not extend the lifespan of an object, thus allowing it to … 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