weak-references

Strong and weak references in Swift

北城余情 提交于 2019-12-20 09:39:10
问题 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? 回答1: 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

IBOutlet and viewDidUnload under ARC

最后都变了- 提交于 2019-12-20 08:37:46
问题 There is a similar question to this on SO here, however I just want to clarify something that wasn't fully explained there. I understand that all delegates and outlets - in fact any reference to a "parent" object, to be a good citizen and think about the object graph for a minute - should be zeroing weak references. Due to the nature of zeroing weak pointers automatically dropping to nil on the referenced object's retain count reaching zero, does this mean that setting IBOutlets to nil in

Best time to cull WeakReferences in a collection in .NET

99封情书 提交于 2019-12-20 02:35:23
问题 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

EXC_BAD_ACCESS on objc_setAssociatedObject with -weak_library /usr/lib/libSystem.B.dylib linker flags

二次信任 提交于 2019-12-19 02:46:09
问题 I have a EXC_BAD_ACCESS when I call objc_setAssociatedObject with the linker flags : -weak_library /usr/lib/libSystem.B.dylib linker flags. I absolutely need the linker flag because of this, do somebody know a workaround? (I also have a crash on dispatch_async but I can work around that... Also, I'm building a iOS 4 only feature on my app that is iOS 3 compatible) EDIT: With more research, I found this and this question but no more answer... 回答1: The problem is simply a bug in the iOS

ArrayList<WeakReference<Runnable>> - How to tidy up best?

喜你入骨 提交于 2019-12-19 02:38:48
问题 A quick question in between: I have a simple WeakRunnableList. Is this way ok to clean it up (removing dead references), or is there a more elegant and faster solution. Full source for my WeakRunnableList: public class WeakRunnableList { private ArrayList<WeakReference<Runnable>> _items = new ArrayList<WeakReference<Runnable>>(); public void Add(Runnable r) { _items.add(new WeakReference<Runnable>(r)); } public void Execute() { ArrayList<WeakReference<Runnable>> remove = new ArrayList

In Swift, unowned vs. weak reference [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-18 12:45:54
问题 This question already has answers here : What is the difference between a weak reference and an unowned reference? (7 answers) Closed 3 years ago . If you have a Dog with a weak reference to Bone , that means that the Dog is the 'owner' of the reference in this situation, and it USES bone, but bone can go out of existence and Dog can still function (because the reference to bone is an optional). However with 'unowned', it seems to be that the keyword 'unowned' is used not in the owner's

Android Handler changing WeakReference

▼魔方 西西 提交于 2019-12-18 12:06:10
问题 My static handler has a WeakReference to my Activity (this is to prevent the well documented memory leak issue). I post a long delayed message and I want this message delivered to my activity (which should be in the foreground). My concern is that on orientation change, my activity is destroyed and the handler has a reference to the old activity which should have been destroyed. In order to get around this in my onCreate for the activity I do this. if(mHandler == null) mHandler = new

How to avoid memory leaks in callback?

拥有回忆 提交于 2019-12-18 11:50:03
问题 Effective Java says: A third common source of memory leaks is listeners and other callbacks. If you implement an API where clients register callbacks but don’t deregister them explicitly, they will accumulate unless you take some action. 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. I am a beginner in Java. Could somebody teach me how to create weak references in

Compacting a WeakReference Dictionary

℡╲_俬逩灬. 提交于 2019-12-18 11:34:23
问题 I've got a class Foo with a property Id . My goal is that there are no two instances of Foo with the same Id at the same time. So I created a factory method CreateFoo which uses a cache in order to return the same instance for the same Id . static Foo CreateFoo(int id) { Foo foo; if (!cache.TryGetValue(id, out foo)) { foo = new Foo(id); foo.Initialize(...); cache.Put(id, foo); } return foo; } The cache is implemented as a Dictionary<TKey,WeakReference>, based on @JaredPar 's Building a

Why do we need weak reference in java

时光怂恿深爱的人放手 提交于 2019-12-18 10:53:06
问题 I understand that weak references are at the mercy of the garbage collector, and we cannot guarantee that the weak reference will exist. I could not see a need to have weak reference, but sure there should be a reason. Why do we need weak reference in java? What are the practical (some) uses of weak reference in java? If you can share how you used in your project it will be great! 回答1: It's actually quite often a bad idea to use weak hashmaps. For one it's easy to get wrong, but even worse it