2010-10-08 94 views
2

我正在從Path方法寫一個創建CGImageRef。這是:垃圾收集器和核心圖形

- (CGImageRef)createImage:(NSString*)path 
{ 
    // Create NSURL 
    NSURL *url = [NSURL fileURLWithPath:path]; 
    CFURLRef cfURL = (CFURLRef)url; 

    // Create image from source 
    CGImageRef image = NULL; 
    CGImageSourceRef imageSource = NULL; 
    imageSource = CGImageSourceCreateWithURL(cfURL, NULL); 
    if(imageSource != NULL) { 
     image = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL); // LEAK 
     CFRelease(imageSource); 
    } else { 
     NSLog(@"Could not load image"); 
     return NULL; 
    } 

    // Scale Image 
    if(image != NULL) { 
     // Create context 
     CGRect rect = CGRectMake(0.0f, 0.0f, CGImageGetWidth(image), CGImageGetHeight(image)); 
     CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); 
     CGContextRef context = CGBitmapContextCreate(NULL, rect.size.width, 
               rect.size.height, 8, 0, 
               colorSpace, 
               kCGImageAlphaPremultipliedFirst); 
     CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
     CGContextDrawImage(context, rect, image); // LEAK 
     CGContextFlush(context); 
     CGColorSpaceRelease(colorSpace); 
     CGImageRelease (image); 

     // Create scaledImage 
    scaledImage = CGBitmapContextCreateImage(context); // LEAK 
    image = scaledImage; 
    CGContextRelease (context); 
    } 

    return image; 
    } 

我已打開垃圾收集器。當我使用Instrument時,發現CGImageSourceCreateImageAtIndex,CGContextDrawImage和CGBitmapContextCreateImage中有泄漏。請問哪裏錯誤?如何在Core Graphics中管理內存當我打開垃圾收集器時?謝謝你,原諒我的英語。

回答

0

泄漏可能發生,因爲您沒有發佈從該方法返回的CGImageRef

+0

感謝戴夫。我釋放返回的CGImageRef。泄漏出現在createImage方法中。 – Fernando 2010-10-08 16:52:09

+1

@Fernando the Leaks工具顯示泄漏內存的創建位置,而不是丟失的位置。換句話說,它顯示你*泄露了什麼,而不是泄漏哪裏。 – 2010-10-08 17:23:28

+0

如果點擊右鍵(View Buttons),我可以查看堆棧跟蹤。 http://cl.ly/ebef2544db1817189bc1在這個堆棧跟蹤中,我在createImage方法中看到了一些錯誤:http://cl.ly/60c121f7e38ea92431bb – Fernando 2010-10-08 18:29:03