【问题标题】:Instruments does not show any leaks but allocations keep increasingInstruments 没有显示任何泄漏,但分配不断增加
【发布时间】:2011-06-20 21:50:41
【问题描述】:

这里有一段代码可以突出显示屏幕的一部分,我想知道这段代码是否有任何泄漏:

- (UIImage*)createImageSection:(UIImage*)image section:(CGRect)section
{
UIScreen *screen = [UIScreen mainScreen];
float originalWidth = image.size.width * [screen scale]  ;
float originalHeight = image.size.height * [screen scale];
int w = originalWidth * section.size.width ;
int h = originalHeight * section.size.height ;


CGContextRef ctx = CGBitmapContextCreate(nil, w , h , 8, w * 8, CGImageGetColorSpace([image CGImage]), kCGImageAlphaPremultipliedLast);
CGContextClearRect(ctx, CGRectMake(0, 0, originalWidth * section.size.width * [screen scale], originalHeight * section.size.height * [screen scale])); // w + h before
CGContextTranslateCTM(ctx, (float)-originalWidth * section.origin.x , (float)-originalHeight * section.origin.y );
CGContextDrawImage(ctx, CGRectMake(0, 0, originalWidth , originalHeight ), [image CGImage]);

CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
//UIImage* resultImage = [[UIImage alloc] initWithCGImage:cgImage scale: [screen scale]];
UIImage* resultImage = [[UIImage alloc] initWithCGImage:cgImage scale:[screen scale] orientation:UIImageOrientationUp] ;
CGContextRelease(ctx);
CGImageRelease(cgImage);
return resultImage ;

}

谢谢 DKV

【问题讨论】:

    标签: iphone memory-leaks instruments


    【解决方案1】:

    自动释放结果图片

    UIImage* resultImage = [[UIImage alloc] initWithCGImage:cgImage scale:[screen scale] orientation:UIImageOrientationUp]
    

    UIImage* resultImage = [[[UIImage alloc] initWithCGImage:cgImage scale:[screen scale] orientation:UIImageOrientationUp] autorelease];
    

    【讨论】:

    • -1:方法定义中包含“create”。为了遵循 Apple 的指导方针,每次调用包含 create、new 或 alloc 的方法时,您有责任释放该对象,不会返回任何自动释放的对象。
    • @lef2,我的糟糕我什至没有看方法 def。谢谢你让我知道。所以,我会收回它。代码中没有泄漏。
    猜你喜欢
    • 2016-01-10
    • 2019-11-11
    • 2010-11-02
    • 2011-10-10
    • 2021-12-25
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多