weak-references

Weak property not zeroing using ARC

泄露秘密 提交于 2019-12-18 09:14:06
问题 I have the following simple code for an object that holds a weak reference: // interface @interface GMWeakRefObj : NSObject @property (weak) id object; @end // implementation @implementation GMWeakRefObj @synthesize object; @end When I run the following test code it fails on the second assert: NSData* d = [[NSData alloc] init]; GMWeakRefObj* weakRef = [[GMWeakRefObj alloc] init]; weakRef.object = d; NSAssert(weakRef.object != nil, @"Reference wasn't assigned"); d = nil; NSAssert(weakRef

C# WeakReference object is NULL in finalizer although still strongly referenced

懵懂的女人 提交于 2019-12-18 04:23:17
问题 Hi I have code here where I don't understand why I hit the breakpoint (see comment). Is this a Microsoft bug of something I don't know or I don't understand properly ? The code was tested in Debug but I think it should not changes anything. Note: You can test the code directly in a console app. JUST FOR INFORMATION... following supercat answer, I fixed my code with proposed solution and it works nicely :-) !!! The bad thing is the usage of a static dict and the performance the goes with it

Do capture lists of inner closures need to redeclare `self` as `weak` or `unowned`?

一笑奈何 提交于 2019-12-18 02:43:16
问题 If I have a closure passed to a function like this: someFunctionWithTrailingClosure { [weak self] in anotherFunctionWithTrailingClosure { [weak self] in self?.doSomething() } } If I declare self as [weak self] in someFunctionWithTrailingClosure 's capture list without redeclaring it as weak again in the capture list of anotherFunctionWithTrailingClosure self is already becoming an Optional type but is it also becoming a weak reference as well? Thanks! 回答1: The [weak self] in

Do capture lists of inner closures need to redeclare `self` as `weak` or `unowned`?

本小妞迷上赌 提交于 2019-12-18 02:43:06
问题 If I have a closure passed to a function like this: someFunctionWithTrailingClosure { [weak self] in anotherFunctionWithTrailingClosure { [weak self] in self?.doSomething() } } If I declare self as [weak self] in someFunctionWithTrailingClosure 's capture list without redeclaring it as weak again in the capture list of anotherFunctionWithTrailingClosure self is already becoming an Optional type but is it also becoming a weak reference as well? Thanks! 回答1: The [weak self] in

Why can the keyword “weak” only be applied to class and class-bound protocol types

假如想象 提交于 2019-12-17 22:33:46
问题 When I'm declaring variables as weak in Swift, I sometimes get the error message from Xcode: 'weak' may only be applied to class and class-bound protocol types I was just wondering why keyword weak can only applied to class and class-bound protocol types? What is the reason behind it? 回答1: weak is a qualifier for reference types (as opposed to value types, such as struct s and built-in value types). Reference types let you have multiple references to the same object. The object gets

Why doesn't the weakref work on this bound method?

▼魔方 西西 提交于 2019-12-17 16:27:59
问题 I have a project where i'm trying to use weakrefs with callbacks, and I don't understand what I'm doing wrong. I have created simplified test that shows the exact behavior i'm confused with. Why is it that in this test test_a works as expected, but the weakref for self.MyCallbackB disappears between the class initialization and calling test_b? I thought like as long as the instance (a) exists, the reference to self.MyCallbackB should exist, but it doesn't. import weakref class A(object): def

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

我的梦境 提交于 2019-12-17 08:10:12
问题 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

Weak NSString variable is not nil after setting the only strong reference to nil

↘锁芯ラ 提交于 2019-12-17 04:02:22
问题 I have a problem with this code : __strong NSString *yourString = @"Your String"; __weak NSString *myString = yourString; yourString = nil; __unsafe_unretained NSString *theirString = myString; NSLog(@"%p %@", yourString, yourString); NSLog(@"%p %@", myString, myString); NSLog(@"%p %@", theirString, theirString); I'm expecting all pointers to be nil at this time, but they are not and I don't understand why. The first (strong) pointer is nil but the other two are not. Why is that? 回答1: tl; dr:

Weak NSString variable is not nil after setting the only strong reference to nil

与世无争的帅哥 提交于 2019-12-17 04:02:11
问题 I have a problem with this code : __strong NSString *yourString = @"Your String"; __weak NSString *myString = yourString; yourString = nil; __unsafe_unretained NSString *theirString = myString; NSLog(@"%p %@", yourString, yourString); NSLog(@"%p %@", myString, myString); NSLog(@"%p %@", theirString, theirString); I'm expecting all pointers to be nil at this time, but they are not and I don't understand why. The first (strong) pointer is nil but the other two are not. Why is that? 回答1: tl; dr:

When would you use a WeakHashMap or a WeakReference?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 01:34:09
问题 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