weak-references

Why doesn't the weakref work on this bound method?

冷暖自知 提交于 2019-11-27 23:00:20
I have a project where i'm trying to use weakrefs with callbacks, and I don't understand what I'm doing wrong. I have created simplified test that shows the exact behavior i'm confused with. Why is it that in this test test_a works as expected, but the weakref for self.MyCallbackB disappears between the class initialization and calling test_b? I thought like as long as the instance (a) exists, the reference to self.MyCallbackB should exist, but it doesn't. import weakref class A(object): def __init__(self): def MyCallbackA(): print 'MyCallbackA' self.MyCallbackA = MyCallbackA self._testA =

Creating a regular weak-reference in Javascript using WeakMaps

旧街凉风 提交于 2019-11-27 22:52:52
I am trying to do the obvious thing with WeakMaps: I want to create a weak reference. In particular, I want to have a list of event-listeners without that list influencing the life of the listener. So I was very excited to find WeakMaps, until I saw they were only built to satisfy one (fairly rare) use-case, extending objects that were otherwise sealed. I can't think when I ever wanted to do that, but I need lists of listeners all the time. Is this possible to use WeakMaps in some clever way I haven't thought of to do this? Bergi No, it is impossible to use WeakMaps to create a weak reference.

Weak property is set to nil in dealloc but property's ivar is not nil

徘徊边缘 提交于 2019-11-27 20:16:17
I noticed the following in Objective-C with ARC enabled: Let's have simple class A and autosynthesized weak property @interface A @property (nonatomic, weak) id refObject; @end @implementation A @end And second class B with dealloc implemented @interface B @end @implementation B -(void) dealloc { NSLog(@"In dealloc"); } @end And finally somewhere in class A have the following: @implementation A ... -(void) foo { B* b = [B new]; self.refObject = b; // Just use b after the weak assignment // in order to not dealloc 'b' before assignement NSLog(@"%@", b); } ... @end If I set a breakpoint in [B

Is there a SoftHashMap in Java?

依然范特西╮ 提交于 2019-11-27 19:51:30
问题 I know there is a WeakHashMap in java.util , but since it uses WeakReference s for everything, which is only referenced by this Map , referenced objects will get lost on the next GC cycle. So it's nearly useless if you want to cache random data, which is very likely to be requested again without being Hard-linked the rest of the time. The best solution would be a map, which uses SoftReference s instead, but I didn't find one in the Java RT Package. 回答1: Edit (Aug. 2012): It turns out that

Weak events in .NET?

旧街凉风 提交于 2019-11-27 18:01:27
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. Judah Gabriel Himango 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 the Problem With Weak Event Handlers . Ideally, though, Microsoft would introduce the

Why does exist WeakHashMap, but absent WeakSet?

こ雲淡風輕ζ 提交于 2019-11-27 17:47:05
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 ? Martin v. Löwis It's simple: there are use cases for WeakHashMap (in particular, the case where you want to annotate objects with additional properties), but there are no use cases for WeakSets. mart Set<Object> weakHashSet = Collections.newSetFromMap( new WeakHashMap<Object, Boolean>()); As seen

Pros and Cons of Listeners as WeakReferences

痴心易碎 提交于 2019-11-27 17:34:37
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. BegemoT First of all, using WeakReference in listeners lists will give your object different semantic , then using hard references. In hard-reference case addListener(...) means "notify supplied

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

こ雲淡風輕ζ 提交于 2019-11-27 17:33:58
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? 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're about to be garbage-collected. So your ‘weak reference’ becomes a key (eg. integer) in a simple lookup,

Why is the implementation of events in C# not using a weak event pattern by default?

▼魔方 西西 提交于 2019-11-27 17:24:05
问题 This question may lead to speculative answers but I presume there's a well thought design decision behind the implementation of event in c#. The event pattern in c# keeps the subscriber alive as long as the publisher of the event is alive. Thus, if you don't unsubscribe, you're leaking memory (well, not really leaking - but memory remains occupied unnecessarily). If I want to prevent this, I can unsubscribe from events or implement a weak event pattern as proposed at MSDN. With the event

Is there a practical use for weak references? [duplicate]

拥有回忆 提交于 2019-11-27 17:09:05
Possible Duplicate: Weak references - how useful are they? Since weak references can be claimed by the garbage collector at any time, is there any practical reason for using them? If you want to keep a reference to something as long as it is used elsewhere e.g. a Listener, you can use a weak reference. WeakHashMap can be used as a short lived cache of keys to derived data. It can also be used to keep information about objects used else where and you don't know when those objects are discarded. BTW Soft References are like Weak references, but they will not always be cleaned up immediately. The