retain-cycle

Does calling a method inside a block that calls another method referring to self cause a retain cycle?

扶醉桌前 提交于 2019-11-30 14:52:14
Can doFirst cause a retain cycle here? @interface Example : NSObject @property (nonatomic, strong) void (^block)(); @end @implementation Example - (void)doFirst { __weak id weakSelf = self; self.block = ^ { [weakSelf doSecond]; }; self.block(); } - (void)doSecond { self.value = //... // do other stuff involving self } @end dasblinkenlight Unlike blocks, methods are not objects; they cannot hold a permanent reference to objects. Your code would not cause a retain cycle. The fact that the code inside doSecond references self explicitly does not mean that self would get retained an extra time.

What happens to Dispatch Queues when UIViewController is Deallocated?

旧街凉风 提交于 2019-11-30 05:26:31
I am trying to better understand retain cycles, especially relative to Dispatch Queues. I am working with AVFoundation and managing an AVCaptureSession on a sessionQueue: private let sessionQueue = DispatchQueue(label: "com.andrewferrarone.sessionQueue") in a lot of code examples in the apple documentation I see this: self.sessionQueue.async { [unowned self] // } Is the [unowned self] self here necessary? self (the viewController) references self.sessionQueue and the closure dispatched to self.sessionQueue captures self. Is this a reference cycle? self is not referencing the closure, just the

Does calling a method inside a block that calls another method referring to self cause a retain cycle?

烈酒焚心 提交于 2019-11-29 22:30:46
问题 Can doFirst cause a retain cycle here? @interface Example : NSObject @property (nonatomic, strong) void (^block)(); @end @implementation Example - (void)doFirst { __weak id weakSelf = self; self.block = ^ { [weakSelf doSecond]; }; self.block(); } - (void)doSecond { self.value = //... // do other stuff involving self } @end 回答1: Unlike blocks, methods are not objects; they cannot hold a permanent reference to objects. Your code would not cause a retain cycle. The fact that the code inside

recursive block and retain cycles in ARC

只谈情不闲聊 提交于 2019-11-29 21:01:52
EDIT2: No. The suggested answer is about async calls. I want & need synchronous calls, like in a normal, standard recursive call. EDIT: while __unsafe_unretained void (^unsafe_apply)(UIView *, NSInteger) ; compiles without warning or errors, it fails at runtime with a NULL stored into unsafe_apply. However this: - (void) applyToView: (UIView *) view { UIColor * (^colorForIndex)(NSInteger) = ^(NSInteger index) { return [UIColor colorWithHue: ((CGFloat) index / 255.0f) saturation: 0.5f brightness: 0.5f alpha: 1.0f] ; } ; void (^applyColors) (UIView *, NSInteger index) = ^(UIView * view,

Understanding retain cycle in depth

做~自己de王妃 提交于 2019-11-29 20:20:16
Lets say we have three objects: a grandparent, parent and child. The grandparent retains the parent, the parent retains the child and the child retains the parent. The grandparent releases the parent. What will happen in this case ? Unless there is some other reference to the parent or child, they both become orphaned. But the retain cycle between the parent and child prevent either from being released and they become wasted memory. A child should never retain a parent. If anything, use a weak reference in the child to maintain a reference to the parent. Retain Cycle is the condition When 2

Knowing where retain cycles are and removing them

女生的网名这么多〃 提交于 2019-11-29 12:52:51
I was wondering if there was an easy way (or at least a way) to find out where retain cycles exist in your program. Also, if I then know where these retain cycles exist, depending on their types (e.g. variable or closure), how do I make them weak. I need to stop all retain cycles with self (my GameScene) so that it deallocates when I don't need it anymore and I want to restart it. Any tips, advice, answers, and feedback would be greatly appreciated (and providing specific code and examples would be preferred). Thank you. Edit: @Sweeper's answer was just what I was looking for. If you're having

Capturing 'self' strongly in this block is likely to lead to a retain cycle [duplicate]

[亡魂溺海] 提交于 2019-11-28 18:48:25
This question already has an answer here: capturing self strongly in this block is likely to lead to a retain cycle 7 answers I have reqest with block. But the compiler issues a warning "Capturing 'self' strongly in this block is likely to lead to a retain cycle" __weak typeof(self) weakSelf = self; [generalInstaImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:data[@"images"][@"low_resolution"][@"url"]]] placeholderImage:[UIImage imageNamed:@"Default"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { NSLog(@"success");

How to fix “Capturing 'block' strongly in this block is likely to lead to a retain cycle”

不羁的心 提交于 2019-11-28 18:26:45
I am working on this code, which does some lengthy asyncronous operation on the net and when it finishes it triggers a completion block where some test is executed and if a variable get a certain value another lengthy operation should start immediately: -(void) performOperation { void(^completionBlock) (id obj, NSError *err, NSURLRequest *request)= ^(id obj,NSError *err, NSURLRequest *request){ int variable=0; // Do completion operation A //... //... // Do completion operation B //Get the variable value if(variable>0){ [self doLengthyAsynchronousOperationWithCompletionBlock: completionBlock];

Is the weakSelf/strongSelf dance really necessary when referencing self inside a non-retained completion called from a UIViewController?

泪湿孤枕 提交于 2019-11-28 17:27:19
Say I have the following method inside a UIViewController subclass: - (void)makeAsyncNetworkCall { [self.networkService performAsyncNetworkCallWithCompletion:^{ dispatch_async(dispatch_get_main_queue(), ^{ [self.activityIndicatorView stopAnimating]; } }); }]; } I know that the reference to self inside the block results in the UIViewController instance being retained by the block. As long as performAsyncNetworkCallWithCompletion does not store the block in a property (or ivar) on my NetworkService , am I right in thinking there is no retain cycle? I realise this structure above will lead to the

recursive block and retain cycles in ARC

人走茶凉 提交于 2019-11-28 17:15:34
问题 EDIT2: No. The suggested answer is about async calls. I want & need synchronous calls, like in a normal, standard recursive call. EDIT: while __unsafe_unretained void (^unsafe_apply)(UIView *, NSInteger) ; compiles without warning or errors, it fails at runtime with a NULL stored into unsafe_apply. However this: - (void) applyToView: (UIView *) view { UIColor * (^colorForIndex)(NSInteger) = ^(NSInteger index) { return [UIColor colorWithHue: ((CGFloat) index / 255.0f) saturation: 0.5f