weak-references

Where does the weak self go?

落花浮王杯 提交于 2019-11-27 06:27:41
I often do this, let when = DispatchTime.now() + 2.0 DispatchQueue.main.asyncAfter(deadline: when) { beep() } and in one app we often do this tickle.fresh(){ msg in paint() } but if you do this let when = DispatchTime.now() + 2.0 DispatchQueue.main.asyncAfter(deadline: when) { tickle.fresh(){ msg in paint() } } of course you have to do this let when = DispatchTime.now() + 2.0 DispatchQueue.main.asyncAfter(deadline: when) { [weak self] _ in tickle.fresh(){ msg in self?.paint() } } or, maybe this let when = DispatchTime.now() + 2.0 DispatchQueue.main.asyncAfter(deadline: when) { tickle.fresh(){

How does weak_ptr work?

纵然是瞬间 提交于 2019-11-27 06:27:02
I understand how to use weak_ptr and shared_ptr . I understand how shared_ptr works, by counting the number of references in its object. How does weak_ptr work? I tried reading through the boost source code, and I'm not familiar enough with boost to understand all the things it uses. Thanks. Paul Groke shared_ptr uses an extra "counter" object (aka. "shared count" or "control block") to store the reference count. (BTW: that "counter" object also stores the deleter.) Every shared_ptr and weak_ptr contains a pointer to the actual pointee, and a second pointer to the "counter" object. To

Weak reference benefits

帅比萌擦擦* 提交于 2019-11-27 06:22:10
Can someone explain the main benefits of different types of references in C#? Weak references Soft references Phantom references Strong references. We have an application that is consuming a lot of memory and we are trying to determine if this is an area to focus on. Scott Pedersen Soft and phantom references come from Java, I believe. A long weak reference (pass true to C#'s WeakReference constructor) might be considered similar to Java's PhantomReference. If there is an analog to SoftReference in C#, I don't know what it is. Weak references do not extend the lifespan of an object, thus

What is the difference between a __weak and a __block reference?

天涯浪子 提交于 2019-11-27 06:15:48
I'm reading Xcode's documentation, and here is something that puzzles me: __block typeof(self) tmpSelf = self; [self methodThatTakesABlock:^ { [tmpSelf doSomething]; }]; The following is copied from the documentation: A block forms a strong reference to variables it captures. If you use self within a block, the block forms a strong reference to self , so if self also has a strong reference to the block (which it typically does), a strong reference cycle results. To avoid the cycle, you need to create a weak (or __block ) reference to self outside the block, as in the example above. I don't

Generic typeof for weak self references

為{幸葍}努か 提交于 2019-11-27 05:04:21
问题 I am trying to figure out a way to use typeof to create a weak reference to self for use in blocks to avoid retain cycles. When I first read about this it seems that the convention was to use __block typeof(self) bself = self; , which compiles but using __block to avoid retain cycles doesn't work anymore and __weak should be used instead. However __weak typeof(self) bself = self; results in an error: The type 'typeof (self)' (aka 'TUAccountsViewController *const __strong') already has

Is it possible to create a truely weak-keyed dictionary in C#?

大城市里の小女人 提交于 2019-11-27 03:07:07
问题 I'm trying to nut out the details for a true WeakKeyedDictionary<,> for C#... but I'm running into difficulties. I realise this is a non-trivial task, but the seeming inability to declare a WeakKeyedKeyValuePair<,> (where the GC only follows the value reference if the key is reachable) makes it seemingly impossible. There are two main problems I see: Every implementation I've so far seen does not trim values after keys have been collected. Think about that - one of the main reasons for using

Understanding Java's Reference classes: SoftReference, WeakReference, and PhantomReference

情到浓时终转凉″ 提交于 2019-11-27 02:30:00
Can someone explain the difference between the three Reference classes (or post a link to a nice explanation)? SoftReference > WeakReference > PhantomReference , but when would I use each one? Why is there a WeakHashMap but no SoftHashMap or PhantomHashMap ? And if I use the following code... WeakReference<String> ref = new WeakReference<String>("Hello!"); if (ref != null) { // ref can get collected at any time... System.gc(); // Let's assume ref gets collected here. System.out.println(ref.get()); // Now what?! } ...what happens? Do I have to check if ref is null before every statement (this

When should weak references be used?

寵の児 提交于 2019-11-27 01:21:40
问题 I recently came across a piece of Java code with WeakReferences - I had never seen them deployed although I'd come across them when they were introduced. Is this something that should be routinely used or only when one runs into memory problems? If the latter, can they be easily retrofitted or does the code need serious refactoring? Can the average Java (or C#) programmer generally ignore them? EDIT Can any damage be done by over-enthusiastic use of WRs? 回答1: Weak references are all about

Weak references - how useful are they?

别来无恙 提交于 2019-11-27 00:34:07
问题 So I've been mulling over some automatic memory management ideas lately - specifically, I've been looking at implementing a memory manager based on reference counting. Of course, everyone knows that circular references kill naive reference counting. The solution: weak references. Personally, I hate using weak references in this way (there are other more intuitive ways of dealing with this, via cycle detection), but it got me thinking: where else could a weak reference be useful? I figure that

Is Josh Smith's implementation of the RelayCommand flawed?

雨燕双飞 提交于 2019-11-27 00:20:00
Consider the reference Josh Smith' article WPF Apps With The Model-View-ViewModel Design Pattern , specifically the example implementation of a RelayCommand (In Figure 3). (No need to read through the entire article for this question.) In general, I think the implementation is excellent, but I have a question about the delegation of CanExecuteChanged subscriptions to the CommandManager 's RequerySuggested event. The documentation for RequerySuggested states: Since this event is static, it will only hold onto the handler as a weak reference. Objects that listen for this event should keep a