0

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.

Andrew
  • 15,935
  • 28
  • 121
  • 203

3 Answers3

4

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];
alex-i
  • 5,406
  • 2
  • 36
  • 56
  • 2
    Note that he’s leaking `self.imageView` — `-initWithFrame:` returns an owned object that’s being assigned to a `retain` property. –  Sep 10 '11 at 12:54
  • To add to your answer, it must be set to nil in the dealloc method... `self.imageView = nil;` – max_ Sep 10 '11 at 13:01
  • If you set to nil after release then its OK. but you cannot set it to nil before release... – Mohammad Sep 10 '11 at 13:01
  • 1
    Forgetting about everything else for a moment, `[self.imageView release];` by itself causes the incorrect decrement of the reference count because it is [calling release on the object returned by the accessor](http://stackoverflow.com/questions/7262268/why-shouldnt-i-use-the-getter-to-release-a-property-in-objective-c). It should be `[imageView release]`. – albertamg Sep 10 '11 at 13:03
  • so anything which is a retain property i should make autoreleased? for example, i also have a retained nsmutablearray which needs releasing, but that gives the same error when i put the release code in dealloc. – Andrew Sep 10 '11 at 13:06
0

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.

albertamg
  • 28,492
  • 6
  • 64
  • 71
Ramakrishna Guttha
  • 762
  • 1
  • 12
  • 24
  • I took the liberty of improving formatting for you. You may want to have a look at the [Markdown Editing Help](http://stackoverflow.com/editing-help). – albertamg Sep 10 '11 at 15:45
0

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!

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • Where did you see that properties cannot be used in dealloc? – Pier-Olivier Thibault Sep 10 '11 at 13:58
  • Here : http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW1 - the section titled 'Don’t Use Accessor Methods in Initializer Methods and dealloc' – deanWombourne Sep 10 '11 at 14:11
  • But they don't realloc go into much detail there so this thread is also quite interesting : http://www.cocoabuilder.com/archive/cocoa/242096-avoiding-kvo-in-dealloc.html – deanWombourne Sep 10 '11 at 14:14