Weak references are an extremely common pattern in Objective-C for avoiding retain cycles or global retention, and it's not at all clear to me how to avoid leaks in the corresponding Dart code.
For instance, if I want to register an observer using addObserverForName:object:queue:usingBlock: the common pattern—indeed, Apple's docs recommend this—is to have self be a weak reference in the block. Then dealloc can make the required call to removeObserver:, and there's no risk of leaking an object. There's no way that I'm aware of to make a weak reference in Dart, which means I need to instead find some explicit point to remove that observer, and if I don't, or if I mess up and there are cases where it's not called, then I leak the object forever, because there's a retain that Dart doesn't know about or control.
If we don't have some way of replicating the weak reference pattern in Dart, then Dart-Obj-C-FFI code is going to be more error prone than the corresponding Obj-C code, which is very problematic since we don't want an ecosystem of plugins that are much more likely to have leaks than we would without ffigen. Perhaps objective_c should provide a little native weak-wrapper class that on the native side is nothing but a weak id property, and on the Dart side is a templated class that down-casts the access to the template type?
Since I'm probably in the majority in not being familiar with WeakReference, we should prominently document how to replicate this pattern in Dart.
(This is essentially the Obj-C version of #575)
Weak references are an extremely common pattern in Objective-C for avoiding retain cycles or global retention, and it's not at all clear to me how to avoid leaks in the corresponding Dart code.
For instance, if I want to register an observer using
addObserverForName:object:queue:usingBlock:the common pattern—indeed, Apple's docs recommend this—is to haveselfbe a weak reference in the block. Thendealloccan make the required call toremoveObserver:, and there's no risk of leaking an object.There's no way that I'm aware of to make a weak reference in Dart, which means I need to instead find some explicit point to remove that observer, and if I don't, or if I mess up and there are cases where it's not called, then I leak the object forever, because there's a retain that Dart doesn't know about or control.If we don't have some way of replicating the weak reference pattern in Dart, then Dart-Obj-C-FFI code is going to be more error prone than the corresponding Obj-C code, which is very problematic since we don't want an ecosystem of plugins that are much more likely to have leaks than we would without ffigen. Perhapsobjective_cshould provide a little native weak-wrapper class that on the native side is nothing but a weakidproperty, and on the Dart side is a templated class that down-casts the access to the template type?Since I'm probably in the majority in not being familiar with
WeakReference, we should prominently document how to replicate this pattern in Dart.(This is essentially the Obj-C version of #575)