开发者

Incorrect decrement of the reference count of an object

开发者 https://www.devze.com 2023-04-04 03:22 出处:网络
I\'m not sure how to deal with releasing this object: h: @interface AHImageView : UIScrollView { UIImageView *imageView;

I'm not sure how to deal with releasing this object:

h:

@interface AHImageView : UIScrollView
{
UIImageView *imageView;
}
@property (nonatomic, retain) UIImageView *imageView;

.m:

-(id)initWithFrame:(CGRect)frame {
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, sel开发者_C百科f.frame.size.width, self.frame.size.height)];

 [self addSubview:self.imageView];
}

-(void)dealloc {
    [super dealloc];
    self.imageView = nil;
    [self.imageView release];
}

The error i'm getting is:

Incorrect decrement of the reference count of an object that is not owned at this point by the caller

and this error points to the [self.imageView release]; line.


you're calling release on nil. Either remove self.imageView=nil; (releases imageView and sets it to nil) or [imageView release]; (only releases the imageView, but you won't use it further so no reason to set it to nil).

Edit: As @Bavarious said there's a leak here:

self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

you should call it like this instead:

self.imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)] autorelease];


There's two bugs in your dealloc method :

(1) You should put [super dealloc] as the last line in your dealloc

If you call [super dealloc] first, the memory that your object is in will be freed (and possibly used by something else). After that point, you can't use members of your object, they're not yours anymore!

(2) It's good practice not to use a property in your dealloc method. You don't know what else this will cause to happen (other objects might be listening via KVO, subclasses might have overridden the setter to do something else etc).

Your correct dealloc should look like :

- (void)dealloc {
    [imageView release];
    [super dealloc];
}

Hope that helps!


In order avoid the release and leak problem modify the code of dealloc Method like this.

-(void)dealloc
{    
    [imageView release];
    self.imageView = nil;
    [super dealloc];
}

problem solved.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号