OK to use underscore for autosynthesized properties in block (strong reference cycle)

michaelsnowden

Say I have something like this:

@interface Foo : UIViewController

@property (nonatomic, strong) NSString *name;

@end

@implementation Foo

- (void)viewDidLoad
{
    [super viewDidLoad];
    _name = @"Me";
    NSArray *blackList = @[@"John", @"Malcolm", @"Jimmy"];
    [blackList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isEqualToString:_name])  // Is _name just as bad as using self.name?
        {
            NSLog(@"You're on the blacklist!");
            *stop = YES;
        }
    }];
}

@end

The question is on this line: if ([obj isEqualToString:_name])

I know it's bad to refer to self inside a block, and you can get around it by using __weak Foo *weakSelf = self and then refer to weakSelf in the block.

__weak Foo *weakSelf = self;
[blackList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isEqualToString:weakSelf.name])
    {
        NSLog(@"You're on the blacklist!");
        *stop = YES;
    }
}];

However, is this still bad if we don't explicitly write self.name, but write _name?

Sergey Kalinichenko

Is _name just as bad as using self.name?

Yes, it is. In fact, self reference is still there - the compiler inserts it for you.

you can get around it by using __weak Foo *weakSelf = self and then refer to weakSelf in the block.

You can use the same trick here by referring to weakSelf->_name

Note: It is not necessary to use this trick with enumerateObjectsUsingBlock:, because the enumeration finishes and the block gets released in the scope of your method. A threat of retain cycle happens only when you put the block in a variable on the self object, while also referring to self from the block.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to keep a strong reference inside of a block without having a retain cycle

From Dev

Why use copy for block properties?

From Dev

Strong attribute on NSArray and retain cycle

From Dev

Strong Reference class

From Dev

Releasing strong reference in ARC

From Dev

Strong reference cycle: closures vs methods

From Dev

When to weak and when to strong reference for nested block in block

From Dev

Use underscore throttle in objects - How to keep object's reference?

From Dev

Strong reference cycles with closures

From Dev

Strong reference to self inside a method running from a block

From Dev

Swift / SpriteKit - Can't break strong reference cycle

From Dev

A "strong" iterator pointer/reference

From Dev

Strong Reference Cycles for Closures?

From Dev

Android: Strong reference cycle using ArrayAdapter

From Dev

Is it a retain cycle if I use a ivar inside block?

From Dev

Why in SDWebImage "strong reference to UIImageView might cause a crash in the nested block"?

From Dev

Is this a strong reference cycle or memory leak at all?

From Dev

Why is a strong reference cycle possible with NSNotificationCenter but not UIView.animateWithDuration?

From Dev

How to keep a strong reference inside of a block without having a retain cycle

From Dev

Changing @property value of self while executing a block that uses a strong reference to self

From Dev

Strong attribute on NSArray and retain cycle

From Dev

break strong reference cycle between Managed Object and Collectionview Cell

From Dev

Swift / SpriteKit - Can't break strong reference cycle

From Dev

Android: Strong reference cycle using ArrayAdapter

From Dev

Is it ok to use a catch block to continue code instead of handling an exception?

From Dev

Avoid strong reference cycle in Block, can I simply do this

From Dev

Why is a strong reference cycle possible with NSNotificationCenter but not UIView.animateWithDuration?

From Dev

Does [self new] in a block creates strong reference cycle?

From Dev

Nested callback strong reference cycle

Related Related

  1. 1

    How to keep a strong reference inside of a block without having a retain cycle

  2. 2

    Why use copy for block properties?

  3. 3

    Strong attribute on NSArray and retain cycle

  4. 4

    Strong Reference class

  5. 5

    Releasing strong reference in ARC

  6. 6

    Strong reference cycle: closures vs methods

  7. 7

    When to weak and when to strong reference for nested block in block

  8. 8

    Use underscore throttle in objects - How to keep object's reference?

  9. 9

    Strong reference cycles with closures

  10. 10

    Strong reference to self inside a method running from a block

  11. 11

    Swift / SpriteKit - Can't break strong reference cycle

  12. 12

    A "strong" iterator pointer/reference

  13. 13

    Strong Reference Cycles for Closures?

  14. 14

    Android: Strong reference cycle using ArrayAdapter

  15. 15

    Is it a retain cycle if I use a ivar inside block?

  16. 16

    Why in SDWebImage "strong reference to UIImageView might cause a crash in the nested block"?

  17. 17

    Is this a strong reference cycle or memory leak at all?

  18. 18

    Why is a strong reference cycle possible with NSNotificationCenter but not UIView.animateWithDuration?

  19. 19

    How to keep a strong reference inside of a block without having a retain cycle

  20. 20

    Changing @property value of self while executing a block that uses a strong reference to self

  21. 21

    Strong attribute on NSArray and retain cycle

  22. 22

    break strong reference cycle between Managed Object and Collectionview Cell

  23. 23

    Swift / SpriteKit - Can't break strong reference cycle

  24. 24

    Android: Strong reference cycle using ArrayAdapter

  25. 25

    Is it ok to use a catch block to continue code instead of handling an exception?

  26. 26

    Avoid strong reference cycle in Block, can I simply do this

  27. 27

    Why is a strong reference cycle possible with NSNotificationCenter but not UIView.animateWithDuration?

  28. 28

    Does [self new] in a block creates strong reference cycle?

  29. 29

    Nested callback strong reference cycle

HotTag

Archive