ARC documentation should highlight UIColor 'gotcha'
| Originator: | Carter | ||
| Number: | rdar://11496104 | Date Originated: | 21-May-2012 11:10 AM |
| Status: | Open | Resolved: | |
| Product: | Documentation | Product Version: | N/A |
| Classification: | Other Bug | Reproducible: | N/A |
Summary:
As the Big Nerd Ranch highlighted on their blog, there is a major 'gotcha' with ARC and the UIColor class. This is major enough that it should be highlighted in the ARC documentation.
The blog post from BNR is here: http://weblog.bignerdranch.com/296-arc-gotcha-unexpectedly-short-lifetimes/
In short, there is a problem in this code running under ARC:
GColorRef color = [UIColor colorWithRed:0.2 green:0.3 blue:0.4 alpha:1.0].CGColor;
[[self.view layer] setBackgroundColor:color];
It crashes. I'm sure experienced programmers that understand ARC semantics can see why, but new developers won't see the problem. Not only that, but this kind of code is *very* common, as creating CGColorRefs directly is more difficult than creating UIColors.
This could even be added to the Clang static analyzer as a mistake ("referencing an unretained property of an autoreleased object may crash"), but that's really a separate issue.
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!
I would say that's a bug in the code for UIColor: you can tag the method with something that says it returns an interior pointer, and ARC will keep it alive longer.