0
votes

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, self.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.

3
that line is completely unnecessaryuser3850

3 Answers

4
votes

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];
0
votes

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!

0
votes

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.