weak-references

Always pass weak reference of self into block in ARC?

纵然是瞬间 提交于 2019-11-26 11:27:24
I am a little confused about block usage in Objective-C. I currently use ARC and I have quite a lot of blocks in my app, currently always referring to self instead of its weak reference. May that be the cause of these blocks retaining self and keeping it from being dealloced ? The question is, should I always use a weak reference of self in a block ? -(void)handleNewerData:(NSArray *)arr { ProcessOperation *operation = [[ProcessOperation alloc] initWithDataToProcess:arr completion:^(NSMutableArray *rows) { dispatch_async(dispatch_get_main_queue(), ^{ [self updateFeed:arr rows:rows]; }); }];

Is it possible to create a “weak reference” in javascript?

跟風遠走 提交于 2019-11-26 10:27:05
问题 Is there any way in javascript to create a \"weak reference\" to another object? Here is the wiki page describing what a weak reference is. Here is another article that describes them in Java. Can anyone think of a way to implement this behavior in javascript? 回答1: There is no language support for weakrefs in JavaScript. You can roll your own using manual reference counting, but not especially smoothly. You can't make a proxy wrapper object, because in JavaScript objects never know when they

Understanding Java's Reference classes: SoftReference, WeakReference, and PhantomReference

纵然是瞬间 提交于 2019-11-26 10:07:48
问题 Can someone explain the difference between the three Reference classes (or post a link to a nice explanation)? SoftReference > WeakReference > PhantomReference , but when would I use each one? Why is there a WeakHashMap but no SoftHashMap or PhantomHashMap ? And if I use the following code... WeakReference<String> ref = new WeakReference<String>(\"Hello!\"); if (ref != null) { // ref can get collected at any time... System.gc(); // Let\'s assume ref gets collected here. System.out.println(ref

When would you use a WeakHashMap or a WeakReference?

ぃ、小莉子 提交于 2019-11-26 09:37:40
The use of weak references is something that I've never seen an implementation of so I'm trying to figure out what the use case for them is and how the implementation would work. When have you needed to use a WeakHashMap or WeakReference and how was it used? One problem with strong references is caching, particular with very large structures like images. Suppose you have an application which has to work with user-supplied images, like the web site design tool I work on. Naturally you want to cache these images, because loading them from disk is very expensive and you want to avoid the

Is Josh Smith&#39;s implementation of the RelayCommand flawed?

送分小仙女□ 提交于 2019-11-26 09:23:15
问题 Consider the reference Josh Smith\' article WPF Apps With The Model-View-ViewModel Design Pattern, specifically the example implementation of a RelayCommand (In Figure 3). (No need to read through the entire article for this question.) In general, I think the implementation is excellent, but I have a question about the delegation of CanExecuteChanged subscriptions to the CommandManager \'s RequerySuggested event. The documentation for RequerySuggested states: Since this event is static, it

How do events cause memory leaks in C# and how do Weak References help mitigate that?

♀尐吖头ヾ 提交于 2019-11-26 08:15:43
问题 There are two ways (that I know of) to cause an unintentional memory leak in C#: Not disposing of resources that implement IDisposable Referencing and de-referencing events incorrectly. I don\'t really understand the second point. If the source object has a longer lifetime than the listener, and the listener doesn\'t need the events anymore when there are no other references to it, using normal .NET events causes a memory leak: the source object holds listener objects in memory that should be

Bitmap, Bitmap.recycle(), WeakReferences, and Garbage Collection

╄→гoц情女王★ 提交于 2019-11-26 08:11:54
问题 AFAIK on Android, it is recommended to reference Bitmap objects as WeakReferences in order to avoid memory leaks. When no more hard references are kept of a bitmap object, the garbage collector will automatically collect it. Now, if I understand correctly, the method Bitmap.recycle() must always be called to free a Bitmap. I think this is because Bitmap objects have special memory management. Is that correct? If this is true, when using WeakReferences, there must be memory leaks because

How do I declare an array of weak references in Swift?

空扰寡人 提交于 2019-11-26 06:58:50
问题 I\'d like to store an array of weak references in Swift. The array itself should not be a weak reference - its elements should be. I think Cocoa NSPointerArray offers a non-typesafe version of this. 回答1: Create a generic wrapper as: class Weak<T: AnyObject> { weak var value : T? init (value: T) { self.value = value } } Add instances of this class to your array. class Stuff {} var weakly : [Weak<Stuff>] = [Weak(value: Stuff()), Weak(value: Stuff())] When defining Weak you can use either struct

How to use WeakReference in Java and Android development?

﹥>﹥吖頭↗ 提交于 2019-11-26 06:55:57
问题 I have been a java developer for 2 years. But I have never wrote a WeakReference in my code. How to use WeakReference to make my application more efficient especially the Android application? 回答1: Using a WeakReference in Android isn't any different than using one in plain old Java. Here is a great guide which gives a detailed explanation: Understanding Weak References. You should think about using one whenever you need a reference to an object, but you don't want that reference to protect

Java: difference between strong/soft/weak/phantom reference

醉酒当歌 提交于 2019-11-26 06:09:56
问题 I have read this article about the topic, but I don\'t really understand it. Please give me some advice along with examples when describing the concepts. 回答1: Java provides two different types/classes of Reference Objects : strong and weak . Weak Reference Objects can be further divided into soft and phantom . Let's go point by point. Strong Reference Object StringBuilder builder = new StringBuilder(); This is the default type/class of Reference Object, if not differently specified: builder