I am having a Navigation based app with a few buttons on the first View (not using ARC). By touching one button optionPressed gets triggered to push to another View.
When I analyse the code for leaks. I get the following warning. “Potential leak of an object” [self.displayViewController setCurrentPhoto:sender.currentTitle];
How should I release the self.displayViewController and where if that’s the cause.
.h
#import <UIKit/UIKit.h>
#import "DisplayViewController.h"
@class DisplayViewController;
@interface Pocket_DjangoViewController : UIViewController
- (IBAction)optionPressed:(UIButton *)sender;
@property (retain, nonatomic) DisplayViewController *displayViewController;
@end
.m
- (IBAction)optionPressed:(UIButton *)sender
{
if (!self.displayViewController) {
self.displayViewController = [[DisplayViewController alloc] initWithNibName:@"DisplayViewController" bundle:nil];
}
[self.displayViewController setCurrentPhoto:sender.currentTitle];
[self.navigationController pushViewController:self.displayViewController animated:YES];
//[self.displayViewController release];
//self.displayViewController = nil;
}
The leak stems for this line:
you should have:
In your actual code, you are creating an object:
which is already retained; then you assign it to a retain property:
and this will create a retain unbalance, as the original alloc is never released and only the retain called by the property is eventually released.