I'm constructing a bitmap image in memory and displaying it as a SwiftUI Image
. Now, the Swift objects should all get cleaned up automatically by Swift's memory management, but there are a couple of calls where Apple's documentation says that the caller is responsible for releasing some memory:
// in application startup code, there's this:
let colorSpace = CGColorSpaceCreateDeviceRGB()
Can I handle releasing this CGColorSpace
by only creating one and letting it get collected when the application exits?
Later on, in a View
struct, I build the Image
whenever the array of pixel information gets updated:
var image: Image {
let context = CIContext()
let ciimage = CIImage(bitmapData: makePixelData(),
bytesPerRow: 4 * width,
size: CGSize(width: width, height: height),
format: .ARGB8,
colorSpace: colorSpace)
// This CGImage comes with documentation that says I'm responsible for releasing it!
if let cgimage = context.createCGImage(ciimage, from: ciimage.extent) {
// At what point is it even safe to dispose of cgimage?
return Image(decorative: cgimage, scale: 1.0)
} else {
// if we're here, it means we couldn't draw the picture, so we should
// display some kind of place holder? Or error?
return Image(systemName: "exclamationmark.triangle.fill")
}
}