Questions tagged [retain-cycle]

A retain cycle is a situation in reference-counted memory management when two (or sometimes more) objects have strong references to each other. Normally, objects are destroyed when their reference count reaches zero, and the references they hold are released at that time. In a cycle, each object keeps the other alive, and neither will be destroyed unless the cycle is deliberately broken.

301 questions
173
votes
11 answers

How to Correctly handle Weak Self in Swift Blocks with Arguments

In my TextViewTableViewCell, I have a variable to keep track of a block and a configure method where the block is passed in and assigned. Here is my TextViewTableViewCell class: // // TextViewTableViewCell.swift // import UIKit class…
NatashaTheRobot
  • 6,879
  • 4
  • 32
  • 27
61
votes
2 answers

Using weak self in dispatch_async function

I read a lot of posts about using __weak self inside dispatch_async, and now I am a litle bit confused. if I have : self.myQueue = dispatch_queue_create("com.biview.core_data", NULL); dispatch_async(self.myQueue, ^(void){ if (!self.var1) { …
Marko Zadravec
  • 8,298
  • 10
  • 55
  • 97
59
votes
10 answers

Understanding retain cycle in depth

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 ?
Tariq
  • 9,861
  • 12
  • 62
  • 103
58
votes
10 answers

Weak Reference to NSTimer Target To Prevent Retain Cycle

I'm using an NSTimer like this: timer = [NSTimer scheduledTimerWithTimeInterval:30.0f target:self selector:@selector(tick) userInfo:nil repeats:YES]; Of course, NSTimer retains the target which creates a retain cycle. Furthermore, self isn't a…
bendytree
  • 13,095
  • 11
  • 75
  • 91
53
votes
7 answers

Is it necessary to use [unowned self] in closures of UIView.animateWithDuration(...)?

UIView.animateWithDuration(1, animations: { [unowned self] in self.box.center = self.boxTopRightPosition }, completion: { [unowned self] completed in self.box.hidden = true }) Is it necessary…
WildCat
  • 1,961
  • 6
  • 24
  • 35
42
votes
2 answers

Referring to weak self inside a nested block

Suppose I already create a weak self using __weak typeof(self) weakSelf = self; [self doABlockOperation:^{ ... }]; Inside that block, if I nest another block: [weakSelf doAnotherBlockOperation:^{ [weakSelf doSomething]; } will it…
Enzo Tran
  • 5,750
  • 6
  • 31
  • 36
40
votes
4 answers

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

Say I have the following method inside a UIViewController subclass: - (void)makeAsyncNetworkCall { [self.networkService performAsyncNetworkCallWithCompletion:^{ dispatch_async(dispatch_get_main_queue(), ^{ …
29
votes
3 answers

Do we need to use __weak self inside UIAnimationBlocks in ARC?

Do we need to use __weak self inside UIAnimation Blocks as given below? Whether it will create retain cycle issue if we are not specifying self as weak? [UIView animateWithDuration:animationDuration delay:0 …
arango_86
  • 4,236
  • 4
  • 40
  • 46
22
votes
3 answers

recursive block and retain cycles in ARC

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…
21
votes
2 answers

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

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…
17
votes
2 answers

Should I use [weak self] in PromiseKit blocks?

PromiseKit states the following on their website: Should I be concerned about retain cycles? tl;dr: it’s safe to use self in promise handlers. This is safe: somePromise.then { self.doSomething() } Provided somePromise resolves, the function…
Senseful
  • 86,719
  • 67
  • 308
  • 465
17
votes
2 answers

unowned vs. weak. Why we should prefer unowned?

As Apple said in "The Swift Programming Language", it seems we should prefer unowned than weak whenever possible: If the captured reference will never become nil, it should always be captured as an unowned reference, rather than a weak…
onevcat
  • 4,591
  • 1
  • 25
  • 31
14
votes
4 answers

Can't make weak reference to closure in Swift

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…
user102008
  • 30,736
  • 10
  • 83
  • 104
14
votes
3 answers

Core Data - break retain cycle of the parent context

Let's say we have two entities in a Core Data model: Departments and Employees. The Department has a one-to-many relationship to Employees. I have the following ManagedObjectContexts: - Root: connected to the Persistent Store Coordinator - Main:…
Yvo
  • 18,681
  • 11
  • 71
  • 90
13
votes
1 answer

Swift closures causing strong retain cycle with self

I just want to know if I am understanding this correctly or not. So according to the apple docs when you create a closure as a property of a class instance and that closure references self(The class that created the closure property) this would…
Esko918
  • 1,397
  • 1
  • 12
  • 25
1
2 3
20 21