weak-references

Why isn’t my weak reference cleared right after the strong ones are gone?

社会主义新天地 提交于 2019-11-26 04:25:21
问题 I am a little bit stubborn, but I want to understand weak and strong references well, so that\'s why I\'m asking you once again. Consider this: __weak NSString* mySecondPointer = myText; NSLog(@\"myText: %@\", myText); The result is myText: (null) and it is pretty obvious - weak reference is set to null just after assignment, cause there is no strong reference to the pointed object. But in this case: __strong NSString* strongPtr = [[NSString alloc] initWithFormat:@\"mYTeSTteXt %d\"]; // weak

What is the difference between a weak reference and an unowned reference?

与世无争的帅哥 提交于 2019-11-26 03:25:47
问题 Swift has: Strong References Weak References Unowned References How is an unowned reference different from a weak reference? When is it safe to use an unowned reference? Are unowned references a security risk like dangling pointers in C/C++? 回答1: Both weak and unowned references do not create a strong hold on the referred object (a.k.a. they don't increase the retain count in order to prevent ARC from deallocating the referred object). But why two keywords? This distinction has to do with the

Always pass weak reference of self into block in ARC?

孤者浪人 提交于 2019-11-26 02:25:46
问题 I am a little confused about block usage in Objective-C. I currently use ARC and I have quite a lot of blocks in my app, currently always referring to self instead of its weak reference. May that be the cause of these blocks retaining self and keeping it from being dealloced ? The question is, should I always use a weak reference of self in a block ? -(void)handleNewerData:(NSArray *)arr { ProcessOperation *operation = [[ProcessOperation alloc] initWithDataToProcess:arr completion:^

What's the difference between SoftReference and WeakReference in Java?

一世执手 提交于 2019-11-26 01:29:16
问题 What\'s the difference between java.lang.ref.WeakReference and java.lang.ref.SoftReference ? 回答1: From Understanding Weak References, by Ethan Nicholas: Weak references A weak reference , simply put, is a reference that isn't strong enough to force an object to remain in memory. Weak references allow you to leverage the garbage collector's ability to determine reachability for you, so you don't have to do it yourself. You create a weak reference like this: WeakReference weakWidget = new