ChatGPT解决这个技术问题 Extra ChatGPT

Retain cycle on `self` with blocks

I'm afraid this question is pretty basic, but I think it's relevant to a lot of Objective-C programmers who are getting into blocks.

What I've heard is that since blocks capture local variables referenced within them as const copies, using self within a block can result in a retain cycle, should that block be copied. So, we are supposed to use __block to force the block to deal directly with self instead of having it copied.

__block typeof(self) bself = self;
[someObject messageWithBlock:^{ [bself doSomething]; }];

instead of just

[someObject messageWithBlock:^{ [self doSomething]; }];

What I'd like to know is the following: if this is true, is there a way that I can avoid the ugliness (aside from using GC)?

I like to call my self proxies this just to flip things around. In JavaScript I call my this closures self, so it feels nice and balanced. :)
I wonder is there any equivalent action needs to be done if I am using Swift blocks
@BenLu absolutely! in Swift closures (and functions which get passed around that mention self implicitly or explicitly) will retain self. Sometimes this is desired, and other times it creates a cycle (because the closure itself gets owned by the self (or owned by something self owns). The main reason this happens is because of ARC.
To avoid problems, the appropriate way to define 'self' to be used in a block is '__typeof(self) __weak weakSelf = self;' in order to have a weak reference.

L
Lily Ballard

Strictly speaking, the fact that it's a const copy has nothing to do with this problem. Blocks will retain any obj-c values that are captured when they are created. It just so happens that the workaround for the const-copy issue is identical to the workaround for the retain issue; namely, using the __block storage class for the variable.

In any case, to answer your question, there's no real alternative here. If you're designing your own block-based API, and it makes sense to do so, you could have the block get passed the value of self in as an argument. Unfortunately, this doesn't make sense for most APIs.

Please note that referencing an ivar has the exact same issue. If you need to reference an ivar in your block, either use a property instead or use bself->ivar.

Addendum: When compiling as ARC, __block no longer breaks retain cycles. If you're compiling for ARC, you need to use __weak or __unsafe_unretained instead.


No problem! If this answered the question to your satisfaction, I would appreciate it if you could choose this as the correct answer to your question. If not, please let me know how I can answer your question better.
No problem, Kevin. SO delays you from selecting an answer to a question immediately, so I had to come back a little later. Cheers.
__unsafe_unretained id bself = self;
@JKLaiho: Sure, __weak is fine as well. If you know for a fact that the object cannot be out of scope when the block is invoked then __unsafe_unretained is ever so slightly faster, but in general that won't make a difference. If you do use __weak, make sure to throw it into a __strong local variable and test that for non-nil before doing anything with it.
@Rpranata: Yes. __block's side-effect of not retaining and releasing was purely due to the inability to properly reason about that. With ARC, the compiler gained that ability, and so __block now retains and releases. If you need to avoid that, you need to use __unsafe_unretained, which instructs the compiler to not perform any retains or releases on the value in the variable.
3
3lvis

Just use:

__weak id weakSelf = self;

[someObject someMethodWithBlock:^{
    [weakSelf someOtherMethod];
}];

For more information: WWDC 2011 - Blocks and Grand Central Dispatch in Practice.

https://developer.apple.com/videos/wwdc/2011/?id=308

Note: if that doesn't work you can try

__weak typeof(self)weakSelf = self;

And did you by any chance found it :)?
You can check the video here - developer.apple.com/videos/wwdc/2011/…
Are you able to reference self inside "someOtherMethod"? Would self reference the weakself at that point or would that also create a retain cycle?
Hi @Oren, if you try to reference self inside "someOtherMethod" you would get a Xcode warning. My approach just makes a weak reference of self.
I only got a warning when referencing self directly inside the block. Putting self inside someOtherMethod didn't cause any warnings. Is that because xcode isn't smart enough or is it not an issue? Would referencing self inside someOtherMethod refer to weakSelf already since that's what your'e calling the method on?
z
zoul

This might be obvious, but you only have to do the ugly self alias when you know you’ll get a retain cycle. If the block is just a one-shot thing then I think you can safely ignore the retain on self. The bad case is when you have the block as a callback interface, for example. Like here:

typedef void (^BufferCallback)(FullBuffer* buffer);

@interface AudioProcessor : NSObject {…}
@property(copy) BufferCallback bufferHandler;
@end

@implementation AudioProcessor

- (id) init {
    …
    [self setBufferCallback:^(FullBuffer* buffer) {
        [self whatever];
    }];
    …
}

Here the API does not make much sense, but it would make sense when communicating with a superclass, for example. We retain the buffer handler, the buffer handler retains us. Compare with something like this:

typedef void (^Callback)(void);

@interface VideoEncoder : NSObject {…}
- (void) encodeVideoAndCall: (Callback) block;
@end

@interface Foo : NSObject {…}
@property(retain) VideoEncoder *encoder;
@end

@implementation Foo
- (void) somewhere {
    [encoder encodeVideoAndCall:^{
        [self doSomething];
    }];
}

In these situations I don’t do the self aliasing. You do get a retain cycle, but the operation is short-lived and the block will get out of memory eventually, breaking the cycle. But my experience with blocks is very small and it might be that self aliasing comes out as a best practice in the long run.


Good point. It's only a retain cycle if self is keeping the block alive. In the case of blocks that never get copied, or blocks with a guaranteed limited duration (e.g. the completion block for a UIView animation), you don't have to worry about it.
In principle, you're correct. However, if you were to execute the code in the example, you'd crash. Block properties should always be declared as copy, not retain. If they're just retain, then there's no guarantee they'll get moved off the stack, which means when you go to execute it, it won't be there anymore. (and copying and already-copied block is optimized to a retain)
Ah, sure, a typo. I went through the retain phase a while ago and quickly realized the thing you are saying :) Thanks!
I'm pretty sure retain is completely ignored for blocks (unless they've already moved off the stack with copy).
@Dave DeLong, No, it would not crash since the @property(retain) is used for an object reference only, not the block.. There is no need for a copy to be used here at all..
p
possen

Posting another answer because this was a problem for me too. I originally thought I had to use blockSelf anywhere there was a self reference inside a block. This is not the case, it is only when the object itself has a block in it. And in fact, if you use blockSelf in these cases the object can get dealloc'd before you get the result back from the block and then it will crash when it tries to call it, so clearly you want self to be retained until the response comes back.

First case demonstrates when a retain cycle will occur because it contains a block which is referenced in the block:

#import <Foundation/Foundation.h>

typedef void (^MyBlock)(void);

@interface ContainsBlock : NSObject 

@property (nonatomic, copy) MyBlock block;

- (void)callblock;

@end 

@implementation ContainsBlock
@synthesize block = _block;

- (id)init {
    if ((self = [super init])) {

        //__block ContainsBlock *blockSelf = self; // to fix use this.
        self.block = ^{
                NSLog(@"object is %@", self); // self retain cycle
            };
    }
    return self;
}

- (void)dealloc {
    self.block = nil;
    NSLog (@"ContainsBlock"); // never called.
    [super dealloc];
} 

- (void)callblock {
    self.block();
} 

@end 

 int main() {
    ContainsBlock *leaks = [[ContainsBlock alloc] init];
    [leaks callblock];
    [leaks release];
}

You don't need blockSelf in the second case because the calling object does not have a block in it that will cause a retain cycle when you reference self:

#import <Foundation/Foundation.h>

typedef void (^MyBlock)(void);

@interface BlockCallingObject : NSObject 
@property (copy, nonatomic) MyBlock block;
@end

@implementation BlockCallingObject 
@synthesize block = _block;

- (void)dealloc {
    self.block = nil;
    NSLog(@"BlockCallingObject dealloc");
    [super dealloc];
} 

- (void)callblock {
    self.block();
} 
@end

@interface ObjectCallingBlockCallingObject : NSObject 
@end

@implementation ObjectCallingBlockCallingObject 

- (void)doneblock {
    NSLog(@"block call complete");
}

- (void)dealloc {
    NSLog(@"ObjectCallingBlockCallingObject dealloc");
    [super dealloc];
} 

- (id)init {
    if ((self = [super init])) {

        BlockCallingObject *myobj = [[BlockCallingObject alloc] init];
        myobj.block = ^() {
            [self doneblock]; // block in different object than this object, no retain cycle
        };
        [myobj callblock];
        [myobj release];
    }
    return self;
}
@end

int main() {

    ObjectCallingBlockCallingObject *myObj = [[ObjectCallingBlockCallingObject alloc] init];
    [myObj release];

    return 0;
} 

That's a common misconception and can be dangerous, because blocks that should retain self may not due to people over-applying this fix. This is a good example of avoiding retain cycles in non-ARC code, thanks for posting.
D
Dave R

Remember also that retain cycles can occur if your block refers to another object which then retains self.

I'm not sure that Garbage Collection can help in these retain cycles. If the object retaining the block (which I'll call the server object) outlives self (the client object), the reference to self inside the block will not be considered cyclic until the retaining object itself is released. If the server object far outlives its clients, you may have a significant memory leak.

Since there are no clean solutions, I would recommend the following workarounds. Feel free to choose one or more of them to fix your issue.

Use blocks only for completion, and not for open-ended events. For example, use blocks for methods like doSomethingAndWhenDoneExecuteThisBlock:, and not methods like setNotificationHandlerBlock:. Blocks used for completion have definite ends of lives, and should be released by server objects after they are evaluated. This prevents the retain cycle from living for too long even if it occurs.

Do that weak-reference dance you described.

Provide a method to clean up your object before it's released, which "disconnects" the object from server objects that may hold references to it; and call this method before calling release on the object. While this method is perfectly fine if your object only has one client (or is a singleton within some context), but will break down if it has multiple clients. You're basically defeating the retain-counting mechanism here; this is akin to calling dealloc instead of release.

If you are writing a server object, take block arguments only for completion. Do not accept block arguments for callbacks, such as setEventHandlerBlock:. Instead, fall back to the classic delegate pattern: create a formal protocol, and advertise a setEventDelegate: method. Do not retain the delegate. If you don't even want to create a formal protocol, accept a selector as a delegate callback.

And lastly, this pattern should ring alarms:

- (void)dealloc {
    [myServerObject releaseCallbackBlocksForObject:self];
    ...
}

If you're trying to unhook blocks that may refer to self from inside dealloc, you're already in trouble. dealloc may never be called due to the retain cycle caused by references in the block, which means that your object is simply going to leak until the server object is deallocated.


GC does help if you use __weak appropriately.
Tracing garbage collection can of course deal with retain cycles. Retain cycles is only a problem for reference counting environments
Just so everybody knows, garbage collection was deprecated in OS X v10.8 in favour of Automatic Reference Counting (ARC) and is scheduled to be removed in a future version of OS X (developer.apple.com/library/mac/#releasenotes/ObjectiveC/…).
C
Community

__block __unsafe_unretained modifiers suggested in Kevin's post may cause to the bad access exception in case of block executed in a different thread. It's better use only __block modifier for the temp variable and make it nil after the usage.

__block SomeType* this = self;
[someObject messageWithBlock:^{
  [this doSomething]; // here would be BAD_ACCESS in case of __unsafe_unretained with
                      //  multithreading and self was already released
  this = nil;
}];

Wouldn't be really safer to just use __weak instead of __block to avoid the need of nilling the variable after using it? I mean, this solution is great if you want to break other types of cycles but certainly I don't see any particular advantages for "self" retain cycles on it.
You can't use __weak if your platform target is iOS 4.x. Also sometimes you need that the code in the block has been executed for the valid object, not for nil.
Y
Yuri Solodkin

You can use libextobjc library. It is quite popular, it is used in ReactiveCocoa for example. https://github.com/jspahrsummers/libextobjc

It provides 2 macros @weakify and @strongify, so you can have:

@weakify(self)
[someObject messageWithBlock:^{
   @strongify(self)
   [self doSomething]; 
}];

This prevents a direct strong reference so we don't get into a retain cycle to self. And also, it prevents self from becoming nil half-way, but still properly decrements the retain count. More in this link: http://aceontech.com/objc/ios/2014/01/10/weakify-a-more-elegant-solution-to-weakself.html


Before showing the simplified code it would be better to know whats behind it, everybody should know the real two lines of code.
Κ
Καrτhικ

How about this?

- (void) foo {
     __weak __block me = self;

     myBlock = ^ {
        [[me someProp] someMessage];
     }
     ...
 }

I don't get the the compiler warning anymore.


y
yijiankaka

Block: a retain cycle will occur because it contains a block which is referenced in the block; If you make the block copy and use a member variable,self will retain.