weak-references

Weak events in .NET?

好久不见. 提交于 2019-11-26 19:15:15
问题 If object A listens to an event from object B, object B will keep object A alive. Is there a standard implementation of weak events that would prevent this? I know WPF has some mechanism but I am looking for something not tied to WPF. I am guessing the solution should use weak references somewhere. 回答1: Dustin Campbell from the DidItWith.NET blog examines several of the failed attempts to create weak event handlers, then goes on to show a valid, working, lightweight implementation: Solving

Why does exist WeakHashMap, but absent WeakSet?

删除回忆录丶 提交于 2019-11-26 19:13:51
问题 From J. Bloch A ... source of memory leaks is listeners ... The best way to ensure that callbacks are garbage collected promptly is to store only weak references to them, for instance, by storing them only as keys in a WeakHashMap. So, why there isn't any WeakSet in the Java Collections framework? 回答1: Set<Object> weakHashSet = Collections.newSetFromMap( new WeakHashMap<Object, Boolean>()); As seen in Collections.newSetFromMap documentation, passing a WeakHashMap to get a Set. 回答2: So, why

Pros and Cons of Listeners as WeakReferences

折月煮酒 提交于 2019-11-26 19:04:59
问题 What are the pros and cons of keeping listeners as WeakReferences. The big 'Pro' of course is that: Adding a listener as a WeakReference means the listener doesnt need to bother 'removing' itself. Update For those worried about the listener having the only reference to the object, why cant there be 2 methods, addListener() and addWeakRefListener()? those who dont care about removal can use the latter. 回答1: First of all, using WeakReference in listeners lists will give your object different

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

混江龙づ霸主 提交于 2019-11-26 17:57:50
问题 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

Weak NSString variable is not nil after setting the only strong reference to nil

不打扰是莪最后的温柔 提交于 2019-11-26 17:45:59
I have a problem with this code : __strong NSString *yourString = @"Your String"; __weak NSString *myString = yourString; yourString = nil; __unsafe_unretained NSString *theirString = myString; NSLog(@"%p %@", yourString, yourString); NSLog(@"%p %@", myString, myString); NSLog(@"%p %@", theirString, theirString); I'm expecting all pointers to be nil at this time, but they are not and I don't understand why. The first (strong) pointer is nil but the other two are not. Why is that? David Rönnqvist tl; dr: The problem is that the string literal never gets released so your weak pointer still

Why isn’t my weak reference cleared right after the strong ones are gone?

人盡茶涼 提交于 2019-11-26 15:24:02
I am a little bit stubborn, but I want to understand weak and strong references well, so that's why I'm asking you once again. Consider this: __weak NSString* mySecondPointer = myText; NSLog(@"myText: %@", myText); The result is myText: (null) and it is pretty obvious - weak reference is set to null just after assignment, cause there is no strong reference to the pointed object. But in this case: __strong NSString* strongPtr = [[NSString alloc] initWithFormat:@"mYTeSTteXt %d"]; // weak pointer points to the same object as strongPtr __weak NSString* weakPtr = strongPtr; if(strongPtr == weakPtr)

Using __block and __weak

喜你入骨 提交于 2019-11-26 13:03:27
问题 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

Weak reference benefits

偶尔善良 提交于 2019-11-26 12:54:13
问题 Can someone explain the main benefits of different types of references in C#? Weak references Soft references Phantom references Strong references. We have an application that is consuming a lot of memory and we are trying to determine if this is an area to focus on. 回答1: 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#,

Using Java&#39;s ReferenceQueue

◇◆丶佛笑我妖孽 提交于 2019-11-26 12:11:46
问题 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

Where does the weak self go?

三世轮回 提交于 2019-11-26 11:56:42
问题 I often do this, let when = DispatchTime.now() + 2.0 DispatchQueue.main.asyncAfter(deadline: when) { beep() } and in one app we often do this tickle.fresh(){ msg in paint() } but if you do this let when = DispatchTime.now() + 2.0 DispatchQueue.main.asyncAfter(deadline: when) { tickle.fresh(){ msg in paint() } } of course you have to do this let when = DispatchTime.now() + 2.0 DispatchQueue.main.asyncAfter(deadline: when) { [weak self] _ in tickle.fresh(){ msg in self?.paint() } } or, maybe