weak-references

When would you use a WeakHashMap or a WeakReference?

ε祈祈猫儿з 提交于 2019-12-17 01:34:06
问题 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? 回答1: 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

Capturing an Objective-C object weakly within C block without declaring an explicit __weak or __block variable

混江龙づ霸主 提交于 2019-12-13 21:14:00
问题 I am capturing a method-scoped object in a C block and I want to avoid retain cycles. Here is my code: ( balloon is a custom view created within my current method) balloon.onAddedToViewHierarchy = ^{ CGRect globalRect = [[UIApplication sharedApplication].keyWindow convertRect:self.frame fromView:self.superview]; CGRect targetFrame = CGRectMake(20, 0, SCREEN_WIDTH - 40, 60); targetFrame.origin.y = below ? globalRect.origin.y + globalRect.size.height + 10 : globalRect.origin.y - 10 - 60/*height

Garbage collection being successful seems to depend on non-related things

假装没事ソ 提交于 2019-12-13 16:23:04
问题 I'm trying to consistently force objects to be garbage collected, for purposes of writing unit-tests related to weak references. However, GC.Collect() , which I expect to force garbage collection, does not seem to behave consistently. Consider the following example code. static void Main (string [] args) { var obj = new object (); var wRef = new WeakReference (obj); obj = null; GC.Collect (); Console.WriteLine (wRef.IsAlive); // Prints false. Console.ReadKey (); } In the code above the object

Testing/Verifying a WeakReference

走远了吗. 提交于 2019-12-13 11:54:59
问题 I'd like to verify that code setting up a WeakReference does not accidentally hold a strong reference to the referenced object. (Here's an example of how it is easy to accidentally do this.) Does this look like the best way to check for inadvertent strong references? TestObject testObj = new TestObject(); WeakReference wr = new WeakReference(testObj); // Verify that the WeakReference actually points to the intended object instance. Assert.Equals(wr.Target, testObject); // Force disposal of

Why should I use Weak/SoftReference with ReferenceQueue argument?Why cannot I poll original reference and check if null?

别来无恙 提交于 2019-12-13 11:08:09
问题 As I understand working use case of detecting when object was collected and memory already free for weak/soft references it polling this queue and when reference appear in queue we can be sure that memory free. WeakReference ref = new WeakReference (new Object()) Why cannot I poll ref and check that it became null ? P.S. according the link provided in comment: If the garbage collector discovers an object that is weakly reachable, the following occurs: 1.The WeakReference object's referent

Is it Possible to Use ConditionalWeakTable with a Tuple Key (or a key comprised of multiple references)?

那年仲夏 提交于 2019-12-13 05:56:41
问题 I am a big fan of the ConditionalWeakTable . It's really quite great. It essentially allows you associate/attach/pair one reference to another reference, all while being thread-safe. It answers a lot of questions/problems for me. Unfortunately, I seem to have a problem I continue to run into, and have not been able to find a good answer to it. Let's say I have a method (denoted by a test method below) where I am using a ConditionalWeakTable , but instead of using a single reference for the

weak object has gone away_what does it mean?

廉价感情. 提交于 2019-12-13 03:38:46
问题 I am using tensorflow for a problem where there is a function which is called once and it works correctly but the second time it is called the error " weak object has gone away" comes up which I don't understand what it means and where the problem might be. The full trackback is as follows: ---------------------------------------------------------------------- TypeError Traceback (most recent call last) ~/.local/share/virtualenvs/tf-tRAPLeXL/lib/python3.6/site-packages/tensorflow_core/python

When will Java WeakHashMap clean null key?

心不动则不痛 提交于 2019-12-12 18:28:18
问题 In the code below nameRef.get() is null , after name = null and System.gc() . import java.lang.ref.WeakReference; public class Main { public static void main(String[] args) { String name = new String("ltt"); WeakReference<String> nameRef = new WeakReference<>(name); System.out.println(nameRef.get()); // ltt name = null; System.gc(); System.out.println(nameRef.get()); // null } } WeakHashMap is based on WeakReference. At last, I think map.size() will be 0. In fact, it's 1. import java.util

WeakReference behaving differently in Debug and Release (no debugger attached). Even when using a factory method

情到浓时终转凉″ 提交于 2019-12-12 16:46:48
问题 [Doh! I am an idiot.. I am rooting the object right there in the code..] I have code that works as expected in Release, but fails in Debug. I have a Dictionary that contains WeakReference instances to other objects. In Release, the dictionary “loses” its values as expected, once they are not referenced and collection occurs. However, in Debug, it doesn’t seem to happen… Even in debug, I do see other WeakReference getting collected in Debug, but the ones in the dictionary are not… The code

Weak-keyed dictionary in Objective-C

折月煮酒 提交于 2019-12-12 13:12:01
问题 I'm wondering if it's possible to have something similar to ActionScript 3's Dictionary object with weak keys in Objective-C. I want to be able to 'attach' an instance of a class to other arbitrary instances. Example; MetaData *meta = [MetaData metaDataForObject:someObject]; meta.whatever = foo; And later: foo = [MetaData metaDataForObject:someObject].whatever; [foo doStuff]; The tricky part is that after the objected referenced by someObject is dealloc'd, I want the object referenced by meta