In almost all my classes I use properties and I always use retain properties like this:
@property (nonatomic, retain) HomeViewController *homeViewController;
And in the implementation file I instantiate these properties like this:
self.homeViewController = [[HomeViewController alloc]init];
and this is the only place I am releasing:
- (void)dealloc
{
[homeViewController release];
[super dealloc];
}
Am I correct in believing that I have a memory leak here – because the retain count will actually be 2. The first comes from the property retain and the second comes from the alloc call?
If yes, should I be using assign under these circumstances?
Yes, you have a leak, and yes your retain count is 2.
Three solutions:
self.homeViewController = [[[HomeViewController alloc]init] autorelease];homeViewController = [[[HomeViewController alloc]init];UIViewController *temp = [[HomeViewController alloc]init]; self.homeViewController = temp; [temp release];