ARC releases UIColor prematurely

Number:rdar://11717864 Date Originated:June 21st, 2012
Status:Duplicate/10542751 Resolved:
Product:iPhone SDK Product Version:5.0
Classification:Serious Bug Reproducible:always

When assigning a UIColor's CGColor to a CGColorRef for later use then ARC discards the UIColor too early when running on device.

Steps to Reproduce:

Run the attached sample project.

Expected Results:

App should run equally well on Simulator and Device. ARC should not release the UIColor before the end of the scope.

Actual Results:

App runs ok on Simulator, but crashes on Device with EXC_BAD_ACCESS.


This was working fine before using it in a project without ARC since the code in question does not use NSObjects for anything except to create the two CGColorRefs.


A workaround is to save the UIColor in an IVAR to establish a strong reference. When doing this the exception no longer occurs. Another workaround apparently is to immediately CGColorRetain the CGColor.


That's why objc_precise_lifetime exists.

This is not a bug.

Believe it or not, it's not a bug: ARC is completely within its rights to deallocate the UIColor when it does. This counterintuitive behavior is nicely explained here:

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at 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!