CGImageRef 属性保留或不保留

发布于 2024-12-18 20:59:29 字数 359 浏览 5 评论 0原文

我有一个关于如何将 CGImageRef 作为类的合成属性处理的问题。 如果我定义一个 CGImageRef ,

@property (nonatomic, retain) CGImageRef image;

那么编译器会抱怨这里不能使用“retain”。如果我省略保留,那么我假设使用“分配”,并且在设置属性时我需要自己保留:

self.image = CGImageRetain ( cgimage );

然后在运行分析时收到“潜在泄漏”警告。我可以安全地忽略这个警告吗?或者,即使属性定义中没有指定“retain”,合成代码仍然会执行隐式 CGRetain 吗?

I have a question on how to handle a CGImageRef as a synthesized property of a class.
If I define an CGImageRef with

@property (nonatomic, retain) CGImageRef image;

then the compiler complains that "retain" cannot be used here. If I leave out the retain, then I assume "assign" is used instead, and I need to do the retain myself when I set the property:

self.image = CGImageRetain ( cgimage );

then I get an "Potential leak" warning when running Analyze. Can I safely ignore this warning? Or does the synthesize code do an implicit CGRetain anyways, even though no "retain" is specified in the property definition?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瘫痪情歌 2024-12-25 20:59:29

您要做的就是向属性添加一个注释,表明该类型确实可以保留。

将属性声明更改为

@property (nonatomic, retain) CGImageRef image __attribute__((NSObject));

注意,这只会为您生成 getter 和 setter,实例变量本身不受 ARC 控制。具体来说,这意味着您必须在dealloc中释放它,并且在直接分配给实例变量时需要使用适当的retain和release。


更好的方法可能是使用 typedef

typedef CGImageRef CGImageObject __attribute__((NSObject));
@property (nonatomic, retain) CGImageObject image;

在这种情况下,实例变量由 ARC 控制,因此您必须 不会dealloc中释放它,并且对实例变量的直接赋值也由ARC处理。


有关参考,请参阅规范,特别是第 4.1.1 节

将 __attribute__((NSObject)) 应用于不可保留的属性
对象指针类型与 ARC 之外的行为相同:
要求属性类型是某种指针并允许
使用 assign 以外的修饰符。这些修饰符仅影响
合成 getter 和 setter;直接访问 ivar(即使
合成)仍然具有原始语义,并且 ivar 中的值
释放期间不会自动释放。

第 3 节

可保留对象指针(或“可保留指针”)是一个值
可保留对象指针类型(“可保留类型”)。有三个
可保留对象指针类型的种类:

  • 块指针(通过将脱字符号 (^) 声明符标记应用于
    函数类型)
  • Objective-C 对象指针(idClassNSFoo* 等)
  • __attribute__((NSObject)) 标记的 typedef

What you want to do is add an annotation to the property that the type really can be retained.

Change the property declaration to

@property (nonatomic, retain) CGImageRef image __attribute__((NSObject));

Note that this will only generate the getters and setters for you, the instance variable itself is not ARC controlled. Specifically, this means that you must release it in dealloc, and that you need to use proper retain and release when assigning directly to the instance variable.


A better approach may be to use a typedef:

typedef CGImageRef CGImageObject __attribute__((NSObject));
@property (nonatomic, retain) CGImageObject image;

In this case, the instance variable is controlled by ARC, and so you must not release it in dealloc, and direct assignments to the instance variable are handled by ARC as well.


For reference, see the specification, specifically section 4.1.1:

Applying __attribute__((NSObject)) to a property not of retainable
object pointer type has the same behavior it does outside of ARC: it
requires the property type to be some sort of pointer and permits the
use of modifiers other than assign. These modifiers only affect the
synthesized getter and setter; direct accesses to the ivar (even if
synthesized) still have primitive semantics, and the value in the ivar
will not be automatically released during deallocation.

and section 3:

A retainable object pointer (or “retainable pointer”) is a value of a
retainable object pointer type (“retainable type”). There are three
kinds of retainable object pointer types:

  • block pointers (formed by applying the caret (^) declarator sigil to a
    function type)
  • Objective-C object pointers (id, Class, NSFoo*, etc.)
  • typedefs marked with __attribute__((NSObject))
半﹌身腐败 2024-12-25 20:59:29

我不喜欢在编译时指示编译器。我认为这很丑。我会自己重写这些方法。

@interface MyClass : NSObject {
    CGImageRef _image;
}
@property (nonatomic, assign) CGImageRef image;
@end

@implementation MyClass
- (void)setImage:(CGImageRef)i {
    if(_image != i) {
        CGImageRelease(_image);
        _image = CGImageRetain(i);
    }
}

- (CGImageRef)image {
    return _image;
}
@end

I don't like to instruct the compiler when compiling. I think it's ugly. I'd override the methods myself.

@interface MyClass : NSObject {
    CGImageRef _image;
}
@property (nonatomic, assign) CGImageRef image;
@end

@implementation MyClass
- (void)setImage:(CGImageRef)i {
    if(_image != i) {
        CGImageRelease(_image);
        _image = CGImageRetain(i);
    }
}

- (CGImageRef)image {
    return _image;
}
@end
晒暮凉 2024-12-25 20:59:29

这个怎么样?

@property (nonatomic, setter=setImage:) CGImageRef image;

(void)setImage:(CGImageRef)image {
    if (_image != image) {
        CGImageRelease(_image);
        _image = CGImageRetain(image);
    }
}

How about this?

@property (nonatomic, setter=setImage:) CGImageRef image;

(void)setImage:(CGImageRef)image {
    if (_image != image) {
        CGImageRelease(_image);
        _image = CGImageRetain(image);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文