I have a Cocoa class that needs to keep a bitmap context around for a long time to do pixel manipulation.
@property (assign, nonatomic) CGContextRef cacheContext; // block of pixels
in my class init:
// this creates a 32bit ARGB context, fills it with the contents of a UIImage and returns a CGContextRef
[self setCacheContext:[self allocContextWithImage:[self someImage]]];
and in dealloc:
CGContextRelease([self cacheContext]);
The Xcode analyzer companies about the init leaking an object of type CGContextRef and in the dealloc there is a complaint about "incorrect decrement of an object that is not owned by the caller".
I believe this is all ok and it runs perfectly.
How can I tell Xcode that this is all ok and not to complain about it?
OK, given the discussion here's what I think would solve the analyzer complaints, let you keep your formal property, and not break any memory management rules.
Declare a read-only property:
@property (readonly) CGContextRef cacheContext;
Assign the ivar directly when it's created
_cacheContext = [self allocContextWithImage:self.someImage];
Release it in dealloc
:
- (void)dealloc
{
CGContextRelease(_cacheContext);
[super dealloc];
}