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
2
votes
1 answer

Will referencing weakSelf from within a method called by weakSelf cause a retain cycle?

I think I understand how simple retain cycles are create but I don't fully understand more complicated situations. Here is code that would cause a retain cycle. (right?) [self.dataController loadInitialWithCompletion:^(BOOL dataChanged) { …
2
votes
2 answers

Are retain cycles okay sometimes?

Say I have an object that should exist as a singleton for the whole life of the app. Is it okay for this object to contain, say, a strong reference to an NSTimer with itself as the timer's target? This will be a retain cycle, but I don't see any…
Bradley Thomas
  • 4,060
  • 6
  • 33
  • 55
2
votes
1 answer

Objective-C How to check which objects are referencing my object (retain cycle)

In a certain portion of code - I am expecting an object to be dellocated but it isn't. Given that object - how can I check which objects are referencing it? Also - Is it possible to know every time an objects reference count goes up? (and by which…
Avba
  • 14,822
  • 20
  • 92
  • 192
2
votes
1 answer

Retain cycles when using ARC and blocks

From my understanding when an object method receives a block as a completion argument I can send "self" in the block: [object doMethodWithCompletion:^{ [self doSomethingWhenThisMethodCompletes] }]; but if this object "retains" the block (saves it…
Avba
  • 14,822
  • 20
  • 92
  • 192
2
votes
1 answer

Correct way to access super in completion block

I have a subclass of NSOperation which send a cancel request over a network. I want to cancel operation only if the request was successful : // overrider cancel of NSOperation -(void)cancel{ [NSURLConnection sendAsynchronousRequest:request …
2
votes
0 answers

Retain Cycle on Retain Cycles

I'm seeing a gradual build up of memory that I think might be a retain cycle. When does this happen: Click on a custom cell that expands and injects a nib with 3 buttons into the expanded area. Clicking the cell again closes the cell, shrinks the…
Emin Israfil iOS
  • 1,801
  • 1
  • 17
  • 26
2
votes
2 answers

IOS block not retain self?

Im new to the block programming in ios, Ive read many guides and they say, things get retained in a block, and I write a demo to test the retain cycle they mentioned. header file: typedef NSString* (^MyBlock)(void); @interface DetailViewController…
nickyu
  • 141
  • 1
  • 11
2
votes
2 answers

EXC_BAD_ACCESS when using weakSelf in block / blocks

I have been struggeling with this issue for a while since i don't think i fully understand the retain cycles. I am totally new to this and i'm trying to learn more about it. I am getting the EXC_BAD_ACCESS message with the following code. I started…
2
votes
3 answers

Class retain counts

Plenty is posted here about avoiding retain cycles with blocks, but what about when using classes and class methods? Say I have a class like this: // MyClass.h + (void)doSomethingAsynch:(void (^)(void))block; + (void)doSomethingElse; and callers…
danh
  • 62,181
  • 10
  • 95
  • 136
1
vote
1 answer

UIVIewController Not Getting Deinitialized When Popping

I am building a settings screen in an app where I have a list of cells. If a user taps on a cell it pushes another controller onto the stack. However, I have this flow in several places in my app. Therefore, I decided to reuse a generic controller…
David Henry
  • 1,972
  • 20
  • 43
1
vote
1 answer

Do we need to explicitly use capture list for weak variables in swift closure?

My understanding for closure was, it will capture all directly referenced objects strongly irrespective of whether the object variable was declared weak or strong outside the closure and, if we want to capture them weakly, then we need to explicitly…
Vishal Singh
  • 4,400
  • 4
  • 27
  • 43
1
vote
2 answers

SwiftUI retain cycle in view hierarchy

I'm having the following view hierarchy which has a retain cycle, that's the simplest I could make to reproduce the issue. All viewmodels and properties has to stay as they are needed in the original solution: import SwiftUI struct MainView: View…
Tamás Horváth
  • 219
  • 2
  • 8
1
vote
1 answer

How box allocated instance is retained in Swift closure?

I'm trying to understand how box allocated instance are retained. On the screen here, we class A { deinit { print("deleted") } } var closure: (() -> Void)! if true { var aa: A? = A() closure = { // Box wraps…
1
vote
0 answers

When using self in the completion block, does it always have a memory leak in Swift?

I've been working on Swift for about half a year, and now I struggle with understanding when to use the [weak self] to avoid a memory leak. TBH, it was too difficult for me to understand from the document or other people's articles, so I used this…
Yuuu
  • 715
  • 1
  • 9
  • 32
1
vote
1 answer

break retain cycle in a block nested in another block

Sometimes I use a block nested in another block, here is my code - (void)test { __weak typeof(self) weakSelf = self; [self.viewSource fetchData:^(BOOL succeed, NSError * _Nonnull error, id _Nonnull data) { __strong typeof(weakSelf)…
childrenOurFuture
  • 1,889
  • 2
  • 14
  • 23