Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ARC - alloc into a strong property. Creates leak?

In ARC, does it create a memory leak to alloc into a @property (strong)

// MyClass.h
@property (strong) NSString *myString;

// MyClass.m
@synthesize myString=_myString;

- (void)viewDidLoad
{
    self.myString = [[NSString alloc] initWithFormat:@"Test %@", otherString];
}

I know that in manual memory management, the equivalent would create a leak

// MyClass.h
@property (retain) NSString *myString;

// MyClass.m
@synthesize myString=_myString;

- (void)viewDidLoad
{
    self.myString = [[NSString alloc] initWithFormat:@"Test %@", otherString];
}

- (void)dealloc
{
    [_myString release];
}

Is ARC able to handle the top example correctly? Does it optimize away one of the retains? Or maybe release twice in the dealloc?

like image 940
bearMountain Avatar asked Nov 17 '11 20:11

bearMountain


1 Answers

This is perfectly valid under ARC.

I would recommend reading the ARC documentation to get more comfortable with trusting what it has to offer. http://clang.llvm.org/docs/AutomaticReferenceCounting.html and Mike Ash has a great blog post on how it works http://www.mikeash.com/pyblog/friday-qa-2011-09-30-automatic-reference-counting.html

like image 200
Joshua Weinberg Avatar answered Nov 15 '22 22:11

Joshua Weinberg