retain-cycle

Can't make weak reference to closure in Swift

会有一股神秘感。 提交于 2019-12-03 05:44:32
Update: I tried writing it without making it weak, and there doesn't seem to be a leak. So maybe the question is no longer necessary. In Objective-C ARC, when you want to have a closure be able to use itself inside of the closure, the block cannot capture a strong reference to itself, or it will be a retain cycle, so instead you can make the closure capture a weak reference to itself, like so: // This is a simplified example, but there are real uses of recursive closures int (^fib)(int); __block __weak int (^weak_fib)(int); weak_fib = fib = ^(int n) { if (n < 2) return n; else return weak_fib

ARC, self and blocks

前提是你 提交于 2019-12-03 03:11:50
I thought I understood the usage of self in a block that is copied is a no no . But in an attempt to clean my code i enabled a bunch of warnings in Xcode, one called "Sending messages to weak pointers" so now in all my blocks, every time I use my created weakself reference __weak typeof(self) weakself = self; I get this warning: Weak receiver may be unpredictably set to nil a trivial example: __weak typeof(self) weakself = self; [aClass doSomethingInABlock:^{ [weakself doSomething]; //warning. }]; I have seen answers which define a version of self within the block like so: __weak typeof(self)

Different closures giving different results for retain cycles in swift

。_饼干妹妹 提交于 2019-12-02 05:01:39
问题 I am reading Apple's Swift Programming Language Guide. In the part about Strong Reference Cycle for closures, I tried a different type of closure but it did not give the expected output. class HTMLElement { let name: String let text: String? lazy var asHTML : String = { //[unowned self] in if let text = self.text { return "<\(self.name)>\(text)</\(self.name)>" } else { return "<\(self.name) />" } }() init(name: String, text: String? = nil) { self.name = name self.text = text } deinit {

Different closures giving different results for retain cycles in swift

試著忘記壹切 提交于 2019-12-02 01:52:22
I am reading Apple's Swift Programming Language Guide. In the part about Strong Reference Cycle for closures, I tried a different type of closure but it did not give the expected output. class HTMLElement { let name: String let text: String? lazy var asHTML : String = { //[unowned self] in if let text = self.text { return "<\(self.name)>\(text)</\(self.name)>" } else { return "<\(self.name) />" } }() init(name: String, text: String? = nil) { self.name = name self.text = text } deinit { println("\(name) is being deinitialized") } } var paragraph: HTMLElement? = HTMLElement(name: "p", text:

Why specify [unowned self] in blocks where you depend on self being there?

 ̄綄美尐妖づ 提交于 2019-12-01 17:54:19
I want self to be non-nil and I'm sure it will be, during the blocks execution. So why explicitly specify [unowned self] ? object.executeBlock { date = self.lastModified } vs object.executeBlock { [unowned self] in date = self.lastModified } Edit: Well i'm getting down votes so let's try again. Q: Let’s say I have a problem. That problem is that I would like to prevent a reference cycle. I have two options. I could use [unowned self] or I could use [weak self]. My question therefore, is this: from these two options, why would I choose [unowned self] ? Why not choose [weak self] everytime ?

Why specify [unowned self] in blocks where you depend on self being there?

╄→гoц情女王★ 提交于 2019-12-01 17:43:32
问题 I want self to be non-nil and I'm sure it will be, during the blocks execution. So why explicitly specify [unowned self] ? object.executeBlock { date = self.lastModified } vs object.executeBlock { [unowned self] in date = self.lastModified } Edit: Well i'm getting down votes so let's try again. Q: Let’s say I have a problem. That problem is that I would like to prevent a reference cycle. I have two options. I could use [unowned self] or I could use [weak self]. My question therefore, is this:

Do I need to use a weak self pointer if a method called from a Block uses self?

坚强是说给别人听的谎言 提交于 2019-12-01 13:29:57
Using self. in blocks causes retain cycles, so I need to create a reference to weakSelf . I understand this BUT! If from my block I call a method which uses self ", does this too cause a retain cycle? For instance, if I reload a UITableView from a block and in some of my UITableView delegate methods I call self. , am I causing a retain cycle? Does that mean I have to pass around this weak reference everywhere? Seems hokey. I might be misreading your question, but your wording: If from my block I call a method which uses "self.", does this too cause a retain cycle? For instance if I reload a

Why retain count is diffrent in debug mode and in running mode?

女生的网名这么多〃 提交于 2019-12-01 10:47:12
问题 I know that how ARC and MRC works. but I am confuse while testing the below code. I don't know why this happen. Why the retain count is different in debug mode and in running mode for the same question? NSMutableArray *a = [NSMutableArray array]; [a addObject:@"abc"]; NSLog(@" 1 Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)a)); __weak NSMutableArray *b = a; NSLog(@" 2 Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)a)); a = nil; NSLog(@" 3 Retain count is %ld",

Do I need to use a weak self pointer if a method called from a Block uses self?

点点圈 提交于 2019-12-01 10:18:19
问题 Using self. in blocks causes retain cycles, so I need to create a reference to weakSelf . I understand this BUT! If from my block I call a method which uses self ", does this too cause a retain cycle? For instance, if I reload a UITableView from a block and in some of my UITableView delegate methods I call self. , am I causing a retain cycle? Does that mean I have to pass around this weak reference everywhere? Seems hokey. 回答1: I might be misreading your question, but your wording: If from my

Potential memory leak in code

喜欢而已 提交于 2019-12-01 06:46:19
问题 In an app I'm working on, there is a requirement to periodically poll for device's data like acceleration, gyro and motion. I wrote the following class to handle all the related tasks (I also use the third-party library SOMotionDetector to detect whether the device is moving. if only then, I call the didReceiveAcceleration delegate method). import CoreMotion import Foundation import SOMotionDetector protocol MotionManagerDelegate: class { func didReceiveAcceleration(_ acceleration: (x: Double