Xcode 11-12, MacOS Catalina, Swift 5 project...
This code is produced "Context leak detected, msgtracer returned -1" error:
import Foundation
import SpriteKit
import GameKit
func getTextureImage(_ index: Int, isSky: Bool = true) -> CGImage {
let perlinSource = GKPerlinNoiseSource(
frequency: 1,
octaveCount: 7,
persistence: 0.5,
lacunarity: 2.0,
seed: 123
)
let noise = GKNoise(perlinSource)
noise.gradientColors = [
-1.0: NSColor(red: 0.0, green: 0.15, blue: 0.85, alpha: 1.0),
1.0 : NSColor(red: 1, green: 1, blue: 1, alpha: 1.0),
]
noise.move(by: vector_double3(Double(index), 0, 0))
let noiseMap = GKNoiseMap(noise,
size: SIMD2(repeating: 1),
origin: SIMD2(repeating: 0.0),
sampleCount: SIMD2(repeating: 16),
seamless: false)
let t = SKTexture(noiseMap: noiseMap)
let i = t.cgImage()
return i
}
for x in 0...128 {
let texture = getTextureImage(x)
}
How to solve that error at line with .cgImage()
method call?
I need CGImage data from colored GKNoiseMap, then I will use it with MetalKit to load data into MTLTexture with
MTKTextureLoader.newTexture(cgImage: texture, options: textureLoaderOptions)
Yeahh, of course I tried
autoreleasepool {
let t = getTextureImage(x)
}
and solved problem :) So, you have to use autoreleasepool
with NSImage and CGImage in Swift API