Crash when creating gradient for CGImage: copy_read_only: vm_copy failed: status 1

Viewed 457

Began crashing with EXC_BAD_ACCESS when building app after new Xcode 11.4 update. Only crashes when building with release build configuration, not debugging configuration.

Breaks at: let img = ctx.makeImage()!

func image() -> CGImage {
        if let image = generatedImage {
            return image
        }

        let w = Int(size.width * scale)
        let h = Int(size.height * scale)
        let bitsPerComponent: Int = MemoryLayout<UInt8>.size * 8
        let bytesPerPixel: Int = bitsPerComponent * 4 / 8
        let bytesPerRow = w * bytesPerPixel

        var data = [ARGB]()

        for y in 0..<h {
            for x in 0..<w {
                let c = pixelDataForGradient(at: CGPoint(x: x, y: y),
                                             size: CGSize(width: w, height: h),
                                             colors: colors,
                                             locations: locations,
                                             startPoint: startPoint,
                                             endPoint: endPoint)
                data.append(c)
            }
        }

        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue)

        let ctx = CGContext(data: &data, width: w, height: h, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)!


        ctx.interpolationQuality = .none
        ctx.setShouldAntialias(false)
        let img = ctx.makeImage()!
        generatedImage = img
        return img
    }
0 Answers
Related