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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With