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
3
votes
1 answer
Swift closure stored as a variable produces memory leak
I have a retain cycle when defining a closure as a variable.
The variable is defined as below:
public class MyCell: UICollectionViewCell {
public var callback: ((MyCell)->Void)?
}
If I use delegates instead of closures, the retain cycle…

Alberto García
- 340
- 3
- 12
3
votes
1 answer
Accessing Singletons in a Closure = Memory Leak?
Does accessing a singleton within a closure cause a retain cycle?
Specifically something like this example:
class TheSingleton
{
static let shared = TheSingleton() //THE SINGLETON
enum Temperature //An associated enum
{
…

Xavier L.
- 709
- 5
- 21
3
votes
1 answer
Why is [weak self] or [unowned self] not needed in Operation Queue?
After learning about Swift's capture list and how it can be used to avoid retain cycle, I can't help noticing something puzzling about OperationQueue: it doesn't need either [weak self] or [unowned self] to prevent memory leak.
class SomeManager {
…

HuaTham
- 7,486
- 5
- 31
- 50
3
votes
2 answers
Potential memory leak in code
In an app I'm working on, there is a requirement to periodically poll for device's data like acceleration, gyro and motion. I wrote the following class to handle all the related tasks (I also use the third-party library SOMotionDetector to detect…

Isuru
- 30,617
- 60
- 187
- 303
3
votes
2 answers
Add observer to runloop creates retain cycle
let observer = CFRunLoopObserverCreateWithHandler(kCFAllocatorDefault, CFRunLoopActivity.BeforeWaiting.rawValue, false, 0, { (observer, activity) in
self.doSomething()
})
CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer,…

itenyh
- 1,921
- 2
- 23
- 38
3
votes
0 answers
NSOperation completionBlock retain cycle warning?
I have similar code in two parts of my app, xcode gives the warning for one but not the other. From my understanding of the documentation I think there should be no warning in either case.
// In one part of my code I have this
__block…

trapper
- 11,716
- 7
- 38
- 82
3
votes
1 answer
Swift Memory management when pass a closure as parameter to singleton
I'm aware of the fact that closure can create retain cycles if it is assigned to a property of a class and instance properties of the class are used inside of the closure. But
1) what about the closure is not assigned to the class property but…

bob
- 382
- 2
- 17
3
votes
2 answers
Do Swift arrays retain their elements?
I'm porting one part of my objective-c framework where I had my custom MyNotificationCenter class for observing purposes.
The class had a property of type NSArray with all observables which are interested in notifications.
In objective-c, an array…

Centurion
- 14,106
- 31
- 105
- 197
3
votes
3 answers
Is it safe to do something after "delete this" if the "somethings" does not require to access "this"?
for example, I have a class which has a retain count and a release method that can delete self if the retain count is 0:
class MyClass{
public:
void retain(){
this->retainCount++;
}
void release(){
this->retainCount--;
…

ggrr
- 7,737
- 5
- 31
- 53
3
votes
1 answer
Why retain count is diffrent in debug mode and in running mode?
I know that how ARC and MRC works. but I am confuse while testing the below code. I don't know why this happen. Why the retain count is different in debug mode and in running mode for the same question?
NSMutableArray *a = [NSMutableArray…

Crazy Developer
- 3,464
- 3
- 28
- 62
3
votes
0 answers
How to pass weak delegate as function argument in Swift?
I am passing the delegate object as function args , as you can see below:
private func sendHttpRequest(request:NSURLRequest,netWorkDelegate:NetWorkDelegate){
let queue = NSOperationQueue();
NSURLConnection.sendAsynchronousRequest(
…

david
- 3,310
- 7
- 36
- 59
3
votes
1 answer
Will adding the parent object of a child object to the listeners lead to a retain cycle in Node.js?
I'm not really very familiar with retain cycles in Node.js coming more from Objective-C/iOS, but I wrote a bit of code that I'm hoping will not lead to a retain cycle. I'm not sure how intelligently V8 deals with garbage collection or how…

Mikey A. Leonetti
- 2,834
- 3
- 22
- 36
3
votes
1 answer
How does UIView prevent retain cycle?
Subview has a reference to superview, while superview also has reference (subviews) to subview.
I'm wondering why this doesn't cause retain cycle?

David Liu
- 16,374
- 12
- 37
- 38
3
votes
3 answers
iOS blocks - avoid retain cycles
what if I'm using a dispatch_queue inside a block? What's the correct way to avoid retain cycles but also avoid releasing the weak pointers too early?
__weak MyClass *weakSelf = self;
[apiClient fetchData:^(...) {
typeof(self)…

swalkner
- 16,679
- 31
- 123
- 210
3
votes
1 answer
NSOutlineView/NSTreeController not releasing all model objects after removing them
I have a very basic app: a window with an NSOutlineView bound to an NSTreeController. The outline view displays a simple model object (TCCard). I added two buttons so that I can add and remove model objects from the outline view.
Looking at the app…

Mark
- 6,647
- 1
- 45
- 88