【问题标题】:Why is there a "potential leak"?为什么会出现“潜在泄漏”?
【发布时间】:2025-09-02 12:40:02
【问题描述】:

Xcode 的分析器抱怨存在“潜在的对象泄漏”。以下方法中的第一行突出显示:

- (void)retrieveBeginRestoreData {
    self.restoreContext = [self.image newARGBBitmapContext];
    if (!self.restoreContext) self.restoreData = nil;
    CGRect rect = {{0,0},self.image.size};

    CGContextDrawImage(self.restoreContext, rect, self.image.CGImage);
    self.restoreData = CGBitmapContextGetData(self.restoreContext);
}

我有一个这样声明的属性:

@property (nonatomic, assign) CGContextRef restoreContext

newARGBBitmapContext 由以下定义:

- (CGContextRef)newARGBBitmapContext {
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    size_t             bitmapByteCount;
    size_t             bitmapBytesPerRow;

    // Get image width, height. We'll use the entire image.
    size_t pixelsWide = CGImageGetWidth(self.CGImage);
    size_t pixelsHigh = CGImageGetHeight(self.CGImage);

    // Declare the number of bytes per row. Each pixel in the bitmap in this
    // example is represented by 4 bytes; 8 bits each of red, green, blue, and
    // alpha.
    bitmapBytesPerRow   = (pixelsWide * 4);
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

    // Use the generic RGB color space.
    //    colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
    colorSpace = CGColorSpaceCreateDeviceRGB();
    if (colorSpace == NULL)
    {
        fprintf(stderr, "Error allocating color space\n");
        return NULL;
    }

    // Allocate memory for image data. This is the destination in memory
    // where any drawing to the bitmap context will be rendered.
    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL)
    {
        fprintf (stderr, "Memory not allocated!");
        CGColorSpaceRelease( colorSpace );
        return NULL;
    }

    // Create the bitmap context. We want pre-multiplied ARGB, 8-bits
    // per component. Regardless of what the source image format is
    // (CMYK, Grayscale, and so on) it will be converted over to the format
    // specified here by CGBitmapContextCreate.
    context = CGBitmapContextCreate (bitmapData,
                                     pixelsWide,
                                     pixelsHigh,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
    if (context == NULL)
    {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
    }

    // Make sure and release colorspace before returning
    CGColorSpaceRelease( colorSpace );

    return context;
}

我设法通过在头文件中将restoreContext 声明为实例变量来解决此问题; “潜在泄漏”警告消失了。

问题:

  • 首先是什么问题?
  • 当我停止将 restoreContext 声明为属性时,这个问题是如何解决的?
  • 解决restoreContext 被声明为属性的问题的正确方法是什么?

【问题讨论】:

    标签: ios objective-c memory-management core-graphics


    【解决方案1】:

    这一行

    self.restoreContext = [self.image newARGBBitmapContext];
    

    执行以下操作:

    1. 它(可能)创建CGContext 的实例对象。

    2. 由于方法名称以new 开头,因此应用了所有权转移。这意味着接收者(您的代码)负责发布它。

    当第二次运行代码行时,CGContext 已经存在的实例的引用被覆盖而不释放实例,它指向。较旧的实例会泄漏。

    【讨论】:

    • 解决将 restoreContext 声明为属性的问题的正确方法是什么?
    • 嗯,这将包括对 MM 规则的完整解释。但是,也有一些问题: 1. 为什么要使用这个方法名? 2、为什么属性的setter语义是assign?抱歉,您似乎有一个悬空指针,并尝试通过尝试和错误来解决该问题。
    • 我使用这个方法名称是因为所有权转移是由于 C 方法CGBitmapContextCreate。这不是约定吗?我正在使用assign,因为CGContextRef 不是原始类型吗?
    • 啊,好吧。但是,前端方法的语义并不共享后端方法的语义。如果语义发生变化,ARC 会将其转移到 Objective-C 代码上。拥有 CF 对象,您应该阅读此clang.llvm.org/docs/…(我猜CGContext 的创建是已知保留的)或使用桥接。这取决于对象类型,现在我没有时间自己检查它。也许我可以在几个小时内完成。