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.
Questions tagged [retain-cycle]
301 questions
4
votes
3 answers
Do I need to check for nil on my strongSelf = weakSelf assignment inside a block?
For example, I'm using SVInfiniteScrolling (https://github.com/alexanderedge/SVInfiniteScrolling).
I have some code that looks like this...
- (void)initializeInfiniteScrollingForTableView {
__weak MyViewController *weakSelf = self;
…

chris P
- 6,359
- 11
- 40
- 84
4
votes
1 answer
Retain cycle warning on __block variable that is an ivar
I'm subclassing AVQueuePlayer, and in my constructor, where I pass the AVPlayerItem's it needs to play, I want to add an observer on the first item to play.
So I'm using the AVPlayer method addBoundaryTimeObserverForTimes:queue:usingBlock:. Proper…

gadu
- 1,816
- 1
- 17
- 31
4
votes
2 answers
Difference between self.completionBlock = ^{} and (void)(^completionBlock)(void) = ^{}
Recently after following Apple documentation
I used the following conventions to avoid retain cycle issues.
__weak __typeof(self) weak_self = self;
void(^completionBlock)(void) = ^(){
__typeof(self) strong_self = weak_self;
if…

arango_86
- 4,236
- 4
- 40
- 46
4
votes
2 answers
How to property hold a reference to the children nodes with ARC in Cocos2d
I have a custom CCNode class that has a bunch of children nodes, and I want to keep references to the children in order to make some custom transitions.
For instance for the child background the custom class would look like this:
@interface MyNode…

rraallvv
- 2,875
- 6
- 30
- 67
4
votes
2 answers
Objective-C weakself declaration
I was looking through some Apple sample code for their 2014 WWDC session 'Advanced User Interfaces with Collection Views' and came across a weakself declaration that looked like the following:
__weak typeof(&*self) weakself = self;
My question is:…

Stefan Church
- 1,351
- 1
- 10
- 14
4
votes
1 answer
How do I use strong and weak with sibling objects (either can be the parent) in Objective-C ARC?
I have two Objective-C objects that relate to each other in some way. You may think of this as a two-way relationship. With ARC, I understand that the parent should hold a strong reference to its child object, while the child holds a weak…

DJ Tarazona
- 1,769
- 14
- 18
4
votes
1 answer
"Retain'ed block property does not copy the block - use copy attribute instead
I come from a heavy JavaScript-oriented background and I'm transitioning as best I can into Objective-C. Naturally, I always find myself jumping at the opportunity to utilize closure functions in my source code, such as:
@property (nonatomic,…

David Zorychta
- 13,039
- 6
- 45
- 81
4
votes
1 answer
retain cycle when calling perfromBlock on self.managedObjectContext with ARC?
In the code below, do I understand the retain cycle issue correctly and is there going to be a retain cycle?
- (NSError *)importRoute:(NSDictionary *)route {
[self.importContext performBlockAndWait:^{
[NSEntityDescription…

Ron
- 1,610
- 15
- 23
4
votes
1 answer
What might be a strategy to detect a retain cycle in an object hierarchy programmatically?
I'm writing an ARC-enabled framework which creates a hierarchy of objects, not unlike Cocoa's view hierarchy. Each controller object can have several subcontrollers. Controllers may have references to each other, which poses the potential risk of…

CodeSmile
- 64,284
- 20
- 132
- 217
4
votes
2 answers
Calling function in block retain cycle
If a function I called from inside a block references 'self', would that create a retain cycle?
__weak id weakSelf = self;
- (void)firstFunction
{
id strongSelf = weakSelf;
if (!strongSelf) return;
[anObject performBlock:^{
…

Thawe
- 171
- 6
3
votes
2 answers
Weak reference doesn't work as expected when passing it as a method reference
I am already aware of the strong/weak reference concept in swift.
yet after running the next code, and tapping on the Button (and dismissing the screen), the TestViewModel stayed in memory!
I was expecting that using [weak viewmodel] will be enough…

vigdora
- 319
- 4
- 11
3
votes
0 answers
SwiftUI retain cycle with .searchable
I think I found a retain cycle when binding an observable to a .searchable modifier...
to verify: create a new swiftUI app; replace ContentView.swift's content with this:
import SwiftUI
struct ContentView: View {
var body: some View {
…

Ron
- 1,610
- 15
- 23
3
votes
2 answers
Weird retain cycle when using the Coordinator pattern
I am building a new app using MVVM + Coordinators. Specifically, I am using the Coordinator pattern found on https://github.com/daveneff/Coordinator.
On the top level I have an AppCoordinator that can start the child coordinator RegisterCoordinator.…

Kevin Renskers
- 5,156
- 4
- 47
- 95
3
votes
1 answer
Why I get retain circle in the second example?
Why do we get strong reference circle in the second example, why we did not in the first example?
class Test1 {
var name: String = "Ted"
lazy var greeting = { return "Hello \(self.name)" }()
deinit{print("goodby1")} // No retain cycle…

Nalov
- 578
- 5
- 9
3
votes
0 answers
weak and unowned self only?
I have encountered some memory leaks that appear to be related to closures capturing variables not necessarily what appears to be the well-known "self retain cycles" in my code. My question in particular relates to SKAction run blocks, e.g.
let…

Mike Pandolfini
- 532
- 4
- 17