On Jul 27, 2009, at 2:44 PM, David Blanton wrote:
_myDocumentView = [[MyDocumentView alloc] init];
[_myDocumentView retain];
You already own _myDocumentView by virtue of the fact that you used alloc/init, so this extra retain is going to lead to memory leaks unless you release twice in -dealloc (which obviously you don't want to do).
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outErrorcontext = CGBitmapContextCreate (bitmap.m_array, bitmap.m_pixelsx, bitmap.m_pixelsy, 8, bitmap.m_pixelsx * 4, colorSpace, kCGImageAlphaNoneSkipFirst|kCGBitmapByteOrder32Host);CGImageRelease(_myDocumentView->_cgImageRef); _myDocumentView->_cgImageRef = CGBitmapContextCreateImage (context); CGImageRetain(_myDocumentView->_cgImageRef); CGContextRelease(context); CGColorSpaceRelease(colorSpace);
You are treating _myDocumentView as a pointer to a struct. Since you declared as _cgImageRef as @public, it will work, it completely defeats the goal of encapsulation in the language, so you should have a good reason for doing it. If you can't think of a good reason, then instead you should set up getter and setter methods for these members:
- (void)setImage:(CGImageRef)image
{
CFRetain(image);
CFRelease(_cgImageRef);
_cgImageRef = image;
}
- (CGImageRef)image
{
return _cgImageRef;
}
An even better design would probably be to not even create getters and
setters and create a method in your view such as
- (BOOL)setImageFromData:(NSData*)data type:(NSString*)type error: (NSError**)error
{
/* everything in -readFromData goes here
}
Then:
- (BOOL)readFromData:(NSData*)data ofType:(NSString*)typeName error:
(NSError*)outError
{
return [_myDocumentView setImageFromData:data type:typeName
error:outError];
}It's hard to say without knowing your architecture if that's the better design, but it is definitely better if your view is the only thing that uses the imageref, especially since it completely hides the fact that your view uses CGImages and you are now free to use anything else that comes along without affecting the rest of your design.
As to why your ivar becomes nil, that's hard to say without seeing your entire project. But I suspect that if you start using better encapsulation and other OO principals in your design, your problem will disappear.
Regards, -- Dave Carrigan [email protected] Seattle, WA, USA
PGP.sig
Description: This is a digitally signed message part
_______________________________________________ Cocoa-dev mailing list ([email protected]) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [email protected]
