Encode UIColor causes UIDeviceRGBColor leak

Originator:craiggrummitt
Number:rdar://29095801 Date Originated:03-Nov-2016
Status:Open Resolved:17-Nov-2016
Product:iOS SDK Product Version:iOS 10.1
Classification: Reproducible:Yes
 
Summary:
Encoding a custom object with a UIColor in RGB color space causes a memory leak.

Steps to Reproduce:
1. Create custom class with NSCoding, that encodes a UIColor.
2. Instantiate instance variable of custom class with a UIColor using RGBA.
3. Run app and select Debug Memory Graph.
4. Open Issue Navigator, select Runtime, see memory leak.
(See code below)


Expected Results:
Expect to not see this memory leak.

Actual Results:
Memory leak message:
"runtime: Memory Issues – (1 leaked type): 1 instance of UIDeviceRGBColor leaked"

Version:
iOS 10.1

Notes:
Instantiating the UIColor with a type property (i.e. UIColor.red) does not create the leak.


Code to reproduce: (project included in Apple bug reporter)
```
class ColorObject: NSObject, NSCoding {
    var detailsColor:UIColor
    init(detailsColor:UIColor) {
        self.detailsColor = detailsColor
    }
    // MARK: NSCoding
    convenience required init?(coder aDecoder: NSCoder) {
        guard let detailsColor = aDecoder.decodeObject(forKey:"detailsColor") as? UIColor else { return nil }
        self.init(
            detailsColor: detailsColor
        )
    }
    func encode(with aCoder: NSCoder) {
        aCoder.encode(detailsColor, forKey: "detailsColor")
    }
    
}


class ViewController: UIViewController {
    var colorObject:ColorObject = ColorObject(detailsColor: UIColor(red: 1, green: 1, blue: 1, alpha: 1))   //UIDeviceRGBColor Memory leak
    //var colorObject:ColorObject = ColorObject(detailsColor: UIColor.red) //No problem
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}
```

Comments

Problem seems to be resolved.

By craiggrummitt at March 13, 2017, 4 p.m. (reply...)

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!