weak-references

How WeakHashMap works under the hood

眉间皱痕 提交于 2019-12-02 19:32:40
问题 I invesigate WeakHashMap ource code to have more knowledge about WeakReference I have found that entry looks like this: private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> { V value; final int hash; Entry<K,V> next; /** * Creates new entry. */ Entry(Object key, V value, ReferenceQueue<Object> queue, int hash, Entry<K,V> next) { super(key, queue); this.value = value; this.hash = hash; this.next = next; } ... Thus when we create new entry we invoke super(key,

Creating a weak subscription to an IObservable

前提是你 提交于 2019-12-02 19:17:25
What I want to do is ensure that if the only reference to my observer is the observable, it get's garbage collected and stops receiving messages. Say I have a control with a list box on it called Messages and this code behind: //Short lived display of messages (only while the user's viewing incoming messages) public partial class MessageDisplay : UserControl { public MessageDisplay() { InitializeComponent(); MySource.IncomingMessages.Subscribe(m => Messages.Items.Add(m)); } } Which is connecting to this source: //Long lived location for message store static class MySource { public readonly

Indexable weak ordered set in Python

假如想象 提交于 2019-12-02 18:53:26
I was wondering if there is an easy way to build an indexable weak ordered set in Python. I tried to build one myself. Here's what I came up with: """ An indexable, ordered set of objects, which are held by weak reference. """ from nose.tools import * import blist import weakref class WeakOrderedSet(blist.weaksortedset): """ A blist.weaksortedset whose key is the insertion order. """ def __init__(self, iterable=()): self.insertion_order = weakref.WeakKeyDictionary() # value_type to int self.last_key = 0 super().__init__(key=self.insertion_order.__getitem__) for item in iterable: self.add(item)

Strong and weak references in Swift

风流意气都作罢 提交于 2019-12-02 18:44:04
In Objective C you can define a property as having a strong or weak reference like so: @property(strong)... @property(weak)... How is this done in swift? Kaan Dedeoglu Straight from the Swift Language guide : class Person { let name: String init(name: String) { self.name = name } var apartment: Apartment? deinit { println("\(name) is being deinitialized") } } class Apartment { let number: Int init(number: Int) { self.number = number } weak var tenant: Person? deinit { println("Apartment #\(number) is being deinitialized") } } properties are strong by default. But look at the tenant property of

When to use weak references in Python?

帅比萌擦擦* 提交于 2019-12-02 15:09:18
Can anyone explain usage of weak references? The documentation doesn't explain it precisely, it just says that the GC can destroy the object linked to via a weak reference at any time. Then what's the point of having an object that can disappear at any time? What if I need to use it right after it disappeared? Can you please explain them with some good examples? Thanks The typical use for weak references is if A has a reference to B and B has a reference to A. Without a proper cycle-detecting garbage collector, those two objects would never get GC'd even if there are no references to either

How WeakHashMap works under the hood

雨燕双飞 提交于 2019-12-02 10:53:06
I invesigate WeakHashMap ource code to have more knowledge about WeakReference I have found that entry looks like this: private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> { V value; final int hash; Entry<K,V> next; /** * Creates new entry. */ Entry(Object key, V value, ReferenceQueue<Object> queue, int hash, Entry<K,V> next) { super(key, queue); this.value = value; this.hash = hash; this.next = next; } ... Thus when we create new entry we invoke super(key, queue); . It is WeakReference constructor. As far I understand after object will be collected by GC

Clarifications regarding weak references in actionscript listeners

倾然丶 夕夏残阳落幕 提交于 2019-12-02 03:41:28
I understand how weak references work, but I am bit confused regarding it's use in actionscript event listeners. Consider the example below: public class Rectangle extends MovieClip { public function Rectangle() { var screen:Shape=new Shape(); screen.addEventListener(MouseEvent.MOUSE_OUT, new Foo().listen, false, 0, true); addChild(screen); } } public class Foo extends MovieClip { public function listen(e:MouseEvent):void { trace("tracing"); } } Now here, since there is only a weak reference to Foo, would not the event listener Foo be garbage collected if and when the garbage collector runs

How to set CADisplayLink in Swift with weak reference between target and CADisplayLink instance

懵懂的女人 提交于 2019-12-02 01:42:31
In Objective-C, we can init CADisplayLink with Proxy Pattern to break strong reference: WeakProxy *weakProxy = [WeakProxy weakProxyForObject:self]; self.displayLink = [CADisplayLink displayLinkWithTarget:weakProxy selector:@selector(displayDidRefresh:)]; Then, just invalidate the displayLink in dealloc : - (void)dealloc { [_displayLink invalidate]; } However, NSProxy seems can't be inherited in Swift: https://bugs.swift.org/browse/SR-1715 I tried to write like this: weak var weakSelf = self displayLink = CADisplayLink(target: weakSelf!, selector: #selector(displayDidRefresh(dpLink:))) It didn

How to cache with weak references when values refer back to keys?

╄→гoц情女王★ 提交于 2019-12-02 01:21:37
问题 I'm using Guava's Cache<Key, Value> . Whenever Key is no more strongly reachable, the cache entry should be garbage collected (someday...). Using CacheBuilder.weakKeys() would do exactly that, if there weren't a reference from Value back to Key . I could make this reference weak, but this could anytime make my Value quite invalid. I could handle it, but I'd prefer not to. I could use weakValues() , but this could lead to very early evictions, as my values are only referenced for a short time.

Best time to cull WeakReferences in a collection in .NET

两盒软妹~` 提交于 2019-12-01 21:14:59
I have a collection (I'm writing a Weak Dictionary ) and I need to cull the dead WeakReferences periodically. What I've usually seen is checks in the Add and Remove methods that say, "After X modifications to the collection, it's time to cull." This will be acceptable for me, but it seems like there should be a better way. I would really like to know when the GC ran and run my cleanup code immediately after. After all, the GC is probably the best mechanism for determining when is a good time to clean up dead references. I found Garbage Collection Notifications , but it doesn't look like this