weak-references

weak or strong for IBOutlet and other [duplicate]

柔情痞子 提交于 2019-11-27 16:58:25
This question already has an answer here: Should IBOutlets be strong or weak under ARC? 11 answers I have switched my project to ARC, and I don't understand if I have to use strong or weak for IBOutlets. Xcode do this: in interface builder, if a create a UILabel for example and I connect it with assistant editor to my ViewController , it create this: @property (nonatomic, strong) UILabel *aLabel; It uses the strong , instead I read a tutorial on RayWenderlich website that say this: But for these two particular properties I have other plans. Instead of strong , we will declare them as weak .

Good implementation of weak dictionary in .Net

北城余情 提交于 2019-11-27 14:24:38
问题 Where can I find good implementation of IDictionary which uses weak references inside? Dictionary should be holding only weak references to values and eventually clean up itself of dead references. Or should I just write it myself? 回答1: 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. 回答2: You'll need to write it yourself. It should be relatively straight forward, implementing the

WeakReference and event handling

和自甴很熟 提交于 2019-11-27 12:47:03
问题 Is it a good practice to implement event handling through WeakReference if that event is the only thing holding the reference and that we would need the object to be garbage collected? As an argument to this: Folks say that if you subscribe to something it’s your responsibility to unsubscribe and you should do it. 回答1: 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

Understanding ConditionalWeakTable

♀尐吖头ヾ 提交于 2019-11-27 11:34:36
问题 I am trying to understand ConditionalWeakTable. What is the difference between class ClassA { static readonly ConditionalWeakTable<ClassA, OtherClass> OtherClassTable = new ConditionalWeakTable<ClassA, OtherClass>(); } and class ClassB { OtherClass otherClass; } ? What would be the pros and cons of using ClassA or ClassB to reference a nullable field? 回答1: I don't exactly get what you're asking about, I assume though you're asking whether you should use property inside your type or

Java's WeakHashMap and caching: Why is it referencing the keys, not the values?

前提是你 提交于 2019-11-27 10:33:21
Java's WeakHashMap is often cited as being useful for caching. It seems odd though that its weak references are defined in terms of the map's keys, not its values. I mean, it's the values I want to cache, and which I want to get garbage collected once no-one else besides the cache is strongly referencing them, no? In which way does it help to hold weak references to the keys? If you do a ExpensiveObject o = weakHashMap.get("some_key") , then I want the cache to hold on to 'o' until the caller doesn't hold the strong reference anymore, and I don't care at all about the string object "some_key".

Android Asyntask: Use weak reference for context to avoid device rotate screen

依然范特西╮ 提交于 2019-11-27 10:17:57
问题 In Apress Pro Android 4 the author has said that: [...] context of currently running activity will no longer be valid when the device is rotated. [...] One approach is to use a weak reference to the activity instead of a hard reference [...] But the author just suggest this, and does not tell how it is done. Who has done this before please give me an example. 回答1: Somewhere in your AsyncTask you'll want to pass in your activity. Then you'll save that reference in a weak reference. Then you

Knowing where retain cycles are and removing them

僤鯓⒐⒋嵵緔 提交于 2019-11-27 07:29:44
问题 I was wondering if there was an easy way (or at least a way) to find out where retain cycles exist in your program. Also, if I then know where these retain cycles exist, depending on their types (e.g. variable or closure), how do I make them weak. I need to stop all retain cycles with self (my GameScene) so that it deallocates when I don't need it anymore and I want to restart it. Any tips, advice, answers, and feedback would be greatly appreciated (and providing specific code and examples

Using __block and __weak

ⅰ亾dé卋堺 提交于 2019-11-27 07:06:26
I've read over this thread: What does the "__block" keyword mean? which discusses what __block is used for but I'm confused about one of the answers. It says __block is used to avoid retain cycles, but the comments underneath it leave me unsure. I'm using it something like this: self.someProperty = x; //where x is some object (id) __block __weak VP_User *this = self; //begin a callback-style block this.someProperty = nil; Do I need to use both __block and __weak ? Any glaring problems with this way this looks? __block is a storage qualifier. It specifies that the variable should directly be

Using Java's ReferenceQueue

半世苍凉 提交于 2019-11-27 06:49:54
Do SoftReference and WeakReference really only help when created as instance variables? Is there any benefit to using them in method scope? The other big part is ReferenceQueue . Besides being able to track which references are determined garbage, can Reference.enqueue() be used to forcibly register an object for garbage collection? For example, would it be worth to create a method that takes some heavy memory resources (held by strong references) in an object and creating References to enqueue them? Object bigObject; public void dispose() { ReferenceQueue<Object> queue = new ReferenceQueue

Why doesn't .NET have a SoftReference as well as a WeakReference, like Java?

爷,独闯天下 提交于 2019-11-27 06:43:46
问题 I really love WeakReference's. But I wish there was a way to tell the CLR how much (say, on a scale of 1 to 5) how weak you consider the reference to be. That would be brilliant. Java has SoftReference, WeakReference and I believe also a third type called a "phantom reference". That's 3 levels right there which the GC has a different behaviour algorithm for when deciding if that object gets the chop. I am thinking of subclassing .NET's WeakReference (luckily and slightly bizzarely it isn't