weak-references

WeakReference/AsyncTask pattern in android

别来无恙 提交于 2019-11-27 00:06:28
问题 I have a question regarding this simple frequently occurring situation in android . We have a main activity , we invoke an AsyncTask alongwith the reference of the mainactivity , so that that the AsyncTask can update the views on the MainActivity. I will break down the event into steps MainActivity creates an AyncTask , passes its reference to it . AysncTask , starts it's work , downloading ten files for example The user changed the orientation of the device. This results in an orphan pointer

using python WeakSet to enable a callback functionality

和自甴很熟 提交于 2019-11-26 23:20:57
问题 I'm investigating if I can implement an easy callback functionality in python. I thought I might be able to use weakref.WeakSet for this, but there is clearly something I'm missing or have misunderstood. As you can see in the code I first tried with a list of call back methods in 'ClassA' objects, but realized that this would keep objects that have been added to the list of callbacks alive. Instead I tried using weakref.WeakSet but that doesnt do the trick either (at least not en this way).

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

会有一股神秘感。 提交于 2019-11-26 22:31:43
问题 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? 回答1: 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

weak or strong for IBOutlet and other [duplicate]

末鹿安然 提交于 2019-11-26 22:30:30
问题 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

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

一个人想着一个人 提交于 2019-11-26 22:15:23
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 garbage collected. Can you explain how events can cause memory leaks with code in C#, and how I can code

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

百般思念 提交于 2019-11-26 22:08:50
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 Bitmap.recycle() is never called when the WeakReferences are freed. Or, somehow, are WeakReferences

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

青春壹個敷衍的年華 提交于 2019-11-26 21:26:17
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. 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 or class . Also, to help with reaping array contents, you could do something along the lines of: extension

Garbage Collection should have removed object but WeakReference.IsAlive still returning true

北城以北 提交于 2019-11-26 21:15:12
问题 I have a test that I expected to pass but the behavior of the Garbage Collector is not as I presumed: [Test] public void WeakReferenceTest2() { var obj = new object(); var wRef = new WeakReference(obj); wRef.IsAlive.Should().BeTrue(); //passes GC.Collect(); wRef.IsAlive.Should().BeTrue(); //passes obj = null; GC.Collect(); wRef.IsAlive.Should().BeFalse(); //fails } In this example the obj object should be GC'd and therefore I would expect the WeakReference.IsAlive property to return false .

Creating a regular weak-reference in Javascript using WeakMaps

不打扰是莪最后的温柔 提交于 2019-11-26 21:13:14
问题 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

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

好久不见. 提交于 2019-11-26 19:17:32
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. Paolo Maresca 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 is a strong Reference Object. This kind of reference makes the referenced object not eligible