weak-references
Is self captured within a nested function
Unfortunately, only Closures have “Capture List” feature like [weak self]. For nested functions, You have to use normal weak or unowned variables. func myInstanceMethod() { weak var _self = self func nestedFunction(result : Bool) { _self?.anotherInstanceMethod() } functionExpectingClosure(nestedFunction) }
Should ConditionalWeakTable be used for non-compiler purposes?
I don’t see anything wrong with using ConditionalWeakTable. If you need ephemerons, you pretty much have no other choice. I don’t think future .NET versions will be a problem – even if only compilers would use this class, Microsoft still couldn’t change it without breaking compatibility with existing binaries. As for overhead – there certainly … Read more
JVM G1GC’s mixed gc not collecting much old regions
well, you didnt mentioned all arguments you set, but you could try to set -XX:+ScavengeBeforeFullGC and you should also think about your Objects lifecycle. how long do your applications Objects live and what size are the Objects. think about it and take a look at the following arguments -XX:NewRatio=n old/new ration (default 2) -XX:SurvivorRatio=n eden/survivor … Read more
WeakReference and event handling
It is good to get in the habit of unsubscribing from events when you can, but sometimes there isn’t an obvious “cleanup” method where it can be done. We recently posted a blog article on this subject; it includes methods that make it easy to subscribe to an event with a WeakReference.
Generic typeof for weak self references
In the latest clang version Apple clang version 4.0 (tags/Apple/clang-421.1.48) (based on LLVM 3.1svn), i.e. Xcode 4.4+, the __typeof__((__typeof__(self))self) trick is not necessary anymore. The __weak typeof(self) bself = self; line will compile just fine.
IBOutlet and viewDidUnload under ARC
Just doing a bit of research… As I understand it, weak is similar to assign, in that they’re both weak references. However, assign does not create a zeroing reference. i.e. if the object in question is destroyed, and you access that property, you WILL get a BAD_ACCESS_EXCEPTION. Weak properties are automatically zeroed (= nil) when … Read more
Good implementation of weak dictionary in .Net
ConditionalWeakTable Class uses weak keys and automatically removes the key/value entry as soon as no other references to a key exist outside the table.
Android Asyntask: Use weak reference for context to avoid device rotate screen
Somewhere in your AsyncTask you’ll want to pass in your activity. Then you’ll save that reference in a weak reference. Then you can dereference and use it again in onPostExecute. Class member: WeakReference<Activity> weakActivity; Somewhere in AsyncTask, probably either constructor or onPreExecute: weakActivity = new WeakReference<Activity>(activity); In onPostExecute: Activity activity = weakActivity.get(); if (activity != … Read more
Weak references – how useful are they?
Event handlers are a good use case for weak references. The object that fires events needs a reference to the objects to invoke event handlers on, but you typically don’t want the event producer’s reference holding to prevent the event consumers from being GC’d. Rather, you’d want the event producer to have a weak reference, … Read more