开发者

How do I autorelease a CGLayer?

开发者 https://www.devze.com 2023-02-10 07:02 出处:网络
I have CGLayers cached in an NSMutableDictionary where I use them as follows: - (CGLayerRef)getLayerForCacheKey:(CacheKey)cacheKey andProperty:(id)property {

I have CGLayers cached in an NSMutableDictionary where I use them as follows:

- (CGLayerRef)getLayerForCacheKey:(CacheKey)cacheKey andProperty:(id)property {
    NSDictionary *cacheDict = [cacheArray objectAtIndex:cacheKey];
    if (cacheDict) {
        NSValue *encodedLayer = [cacheDict objectForKey:property];
        if (encodedLayer) {
            CGLayerRef returnedLayer = nil;
            [encodedLayer getValue:&returnedLayer];
            return returnedLayer;
        }
    }
    return nil;
}

- (void)saveLayer:(CGLayerRef)layer toCacheWithKey:(CacheKey)cacheKey andProperty:(id)property {
    CGLayerRef layerToSave = CGLayerRetain(layer);
    NSMutableDictionary *cacheDict = [cacheArray objectAtIndex:cacheKey];
    NSValue *encodedLayer = [[NSValue alloc] initWithBytes:&layerToSave objCType:@encode(CGLayerRef)];
    [cacheDict setObject:encodedLayer forKey:property];
    [encodedLayer release];
}

I believe I have a memory leak here by not CGLayerRelease'ing returnedLayer from getLayerForCacheKey. Do we have any method in which they can be autoreleased?

CacheKey key = CacheKeyFretNumberHighlight;
CGLayerRef numberLayer = [cacheManager getLayerForCacheKey:key andProperty:note];
// save to cache
if (!开发者_JAVA技巧numberLayer) {
    numberLayer = CGLayerCreateWithContext(nil, [numberStr sizeWithFont:font], nil);
    CGContextRef numberLayerCtx = CGLayerGetContext(numberLayer);
    UIGraphicsPushContext(numberLayerCtx);
    CGContextSetFillColorWithColor(numberLayerCtx, highlightColor);
    [numberStr drawAtPoint:CGPointZero withFont:font];
    UIGraphicsPopContext();

    [cacheManager saveLayer:numberLayer toCacheWithKey:key andProperty:note];

    CGContextDrawLayerAtPoint(layerCtx, point, numberLayer);
    CGLayerRelease(numberLayer);

} else {
    CGContextDrawLayerAtPoint(layerCtx, point, numberLayer);
}


Unfortunately there’s no CFAutorelease. How about changing the name of the cache retrieval method to make it obvious you are returning a copy?

- (CGLayerRef) copyLayerForCacheKey: (CacheKey) key andProperty: (id) property;

Then even the static analyzer should understand the difference.

0

精彩评论

暂无评论...
验证码 换一张
取 消