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?
What you want to do is add an annotation to the property that the type really can be retained.
Change the property declaration to
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: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:
and section 3: