1
votes

I just ran the clang static analyzer on my project, and I am receiving the following warning:

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

Please can you tell me what my problem is. I usually am able manage the memory used in my app very well.

self.cupboardViewController = [[CupboardViewController alloc] initWithNibName:@"CupboardViewController" bundle:[NSBundle mainBundle]];
[self.window addSubview:self.cupboardViewController.view];

- (void)dealloc {
  [[self cupboardViewController] release];//where I am getting the warning.
  [super dealloc];
}
2
Assuming cupboardViewController is marked retain, you're going to leak it regardless, because it'll automatically retain the new object you create in the first line of your example. You should autorelease that object before assigning it to the property, à la self.cupboardViewController = [[[CupboardViewController alloc] init...] autorelease];. - Jonathan Grynspan
I did that, but it caused the application to crash after a while. - max_

2 Answers

3
votes

Pretty sure you should be releasing the instance variable, not the property.

- (void)dealloc {
  [cupboardViewController release];
  [super dealloc];
}
0
votes

If you have cupboardViewController as a retained property then setting self.cupboardViewController = in the above code creates a retain count of 2.

The problem is its over retained, so when you release it in dealloc there is still an outstanding retain and thus it leaks.

A code standard I use is simply:

theProperty = [[NS* alloc] init]

When I alloc my property (creating a single retain), then just:

[theProperty release];

In my dealloc method.

This way I am consistent in that I don't reference the property, only the iVar and sidestep these issues with over or under retain and release.