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
1
vote
1 answer

MVVM - using closures to bind a ViewModel with a DataSource: capture list needed?

I have a viewController keeping a (strong) reference to its viewModel lazy private var viewModel: ListViewModel = { return ListViewModel() }() override func viewDidLoad() { super.viewDidLoad() initViewModel() } private func…
AppsDev
  • 12,319
  • 23
  • 93
  • 186
1
vote
1 answer

Are memory retain cycles impossible in a single-ViewController-app? (Swift / IOS)

I remember from watching CS193P from Stanford University on YouTube (yes, I'm a smart bunz)... that there's this thing called a memory leak or "retain cycle" -- something really bad -- that can happen when you do things like this: referencing self.…
Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100
1
vote
1 answer

Is cleaning up strong references in deinit a correct pattern?

There are several resources (blog, SO question, plus I've seen it used everywhere) that recommend removing an observer from the NotificationCenter in the deinit of the UIViewController, e.g.: deinit { …
Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
1
vote
1 answer

How to get rid of retain cycle with collectionview

I am having trouble destroying viewcontrollers due what I believe to be a retain cycle between a collectionview and the viewcontroller. I tried making the collectionview a weak variable but I am now getting nil whenever I trying to add the…
Blue
  • 1,408
  • 4
  • 17
  • 36
1
vote
2 answers

Retain cycle in Async calls while updating UI

Following is the code used by me. With this code deinit is not called, but if I comment out this line weakSelf?.tableView.reloadData() from code deinit gets called. Am I doing something wrong? ZLNetworkHelper.sharedManager.getUserSavedAddress {…
Ajay Kumar
  • 1,807
  • 18
  • 27
1
vote
1 answer

Capture semantics when assigning a function to a block in Swift?

I'm wondering about what it means when you assign a function to a block in Swift in terms of memory management (i.e. I want to avoid a retain cycle). For example, say I have a button with the following definition: class Button { var wasTapped: ()…
Senseful
  • 86,719
  • 67
  • 308
  • 465
1
vote
1 answer

do I need to use capture list to avoid retain cycle in following code? As if A is not directly holding B and B is holding back to A

Please have a look. I am not sure that following code is having a retain cycle. I looked other post and article related to retain cycle but still not clear with following code. I intentionally rename classes and other stuff. import UIKit class…
Manish Nahar
  • 834
  • 10
  • 24
1
vote
1 answer

Xcode Instruments can't detect retain cycle for strong delegate type?

I just spent an hour trying to fix a retain cycle in my code. It was basically the view controller not getting deallocated after dismiss. However, when I was using Instruments to check for memory leak, it passed every leak check. Please see the…
Dovahkiin
  • 1,239
  • 11
  • 23
1
vote
1 answer

Why doesn't referencing outside of a function create retain cycle?

Sorry, but I know this is a really dumb question, and I already kind of 'know' the answer, but I need someone to clearly explain to me WHY the answer is what it is. Lately, I've become a bit obsessed/paranoid about retain cycles and memory leaks in…
1
vote
0 answers

why cant i reference a struct in the same struct type

I have a struct the has a property of the same type. I'm getting an error Value type '' cannot have a stored property that references itself But this is not a reference to self. It's just a reference to a value of the same type. If i change it to…
ilan
  • 4,402
  • 6
  • 40
  • 76
1
vote
2 answers

Storing Block of code inside variable

It's pretty simple, i'm sure i'm missing something. I'm trying to understand how to achieve the following: action should "hold" a block of code, that i will eventually execute inside UIView.animate (for example). What is the right way? + Should i…
Roi Mulia
  • 5,626
  • 11
  • 54
  • 105
1
vote
2 answers

Should an asynchronous swift function retain a strong reference to the object?

We implemented and extension to NSData that asynchronously persists the data to a URL. Here is a short version of the function. extension NSData { func writeToURL1(url:NSURL, completion: () -> Void) { …
Sei Flavius
  • 133
  • 1
  • 7
1
vote
0 answers

Obj-C help resolving Retain Cycle in a block

I have a BaseUnit Class which has a method - (void)attackUnit:(__weak BaseUnit *)unit Where it attacks another unit in a loop. When only one unit attacks everything is fine and the dead unit get deallocated, but when both user attacks each other…
Coldsteel48
  • 3,482
  • 4
  • 26
  • 43
1
vote
1 answer

Why using an ivar can cause a retain cycle in reactive cocoa?

I use an ivar in a reactive cocoa's block, set weakify(self) and strongify(self) to break the retain cycle. But when I run the code, It causes a memory leak, the controller doesn't call dealloc method. If I change the ivar to an property, it runs…
Monqi
  • 81
  • 1
  • 6
1
vote
2 answers

Should I make delegate of custom cell as a weakly referenced proprety?

ViewController Code class ViewController: UIViewController { deinit { print("ViewController deinitialised") } @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { self.tableView.dataSource =…
Prajeet Shrestha
  • 7,978
  • 3
  • 34
  • 63