Reputation: 11452
I have been reading different sources for automatic reference counting but haven't found any great article or documentation which clarify my understanding. My understanding about ARC(Automatic Reference Counting) is, it completely takes over developer control on memory management and assign it to compiler to do memory management.
Am I thinking right?
So does that mean retain,release and autorelease is no more for iOS 5 SDK?
Lets say I use to make object like this,
UIView *message = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
message.tag = 0;
[message addSubview:balloonView];
[message addSubview:label];
[cell.contentView addSubview:message];
[balloonView release];
[label release];
[message release];
will become like this IF ARC is ON,
UIView *message = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
message.tag = 0;
[message addSubview:balloonView];
[message addSubview:label];
[cell.contentView addSubview:message];
Any input will be helpful thank you
Cheers!
Upvotes: 0
Views: 1432
Reputation: 4698
Yes and no:
Yes, ARC rids you of manually calling retain
, release
, autorelease
, possibly lets you delete quite a few of your dealloc
implementations and — on the new runtimes — even introduces zeroing weak references (w00t!), but it does not prevent you from leaking, per se.
It is automated retain/release and not a garbage-collector, so it "allows you" to still leak memory by creating retain-cycles.
Furthermore, it changes the reference semantics of the __block
attribute from weak
to strong
:
Every bit of code where you avoided the capture of self
by using __block id blockSelf = self;
is now a potential leak — luckily, Clang has become even better warning you about issues like this.
An area, where ARC actually makes you write more code than before, most of the time, is when you are using toll-free-bridging between CFTypeRef
s and id <NSObject>
s:
The casts have to be annotated to tell ARC what to do or you'll get a compiler error.
If you're working with plain CF APIs, nothing changes: everything stays manual.
One of the best resources on ARC I found, is Chris Parker's WWDC talk on ARC Internals. If you haven't seen it yet, you should definitely check it out — the general part on ARC starts around the 8 minute mark, while the details start about 29 minutes in.
Upvotes: 6
Reputation: 27147
Your example code is correct. ARC makes those calls for you.
It ARC doesn't, however, "completely takes over" a newbie would still need to understand memory management. But like apple says it lets you concentrate on object ownership instead of retain counts.
For example if you don't modify your NSObject <Protocol> *_delegate;
with __weak
or __unsafe_unretained
you will still create a retain cycle.
Upvotes: 1
Reputation: 1795
Yes, you're right. ARC pretty much takes the hassle of doing memory management out of your hands and has the compiler deal with all that stuff. Sit back, relax, and worry more about writing the code you want rather than causing trivial memory management issues :)
Upvotes: 0