Consider the following code in the initialiser of a class:
UIViewController* blankViewController=[[DisplayViewController alloc]
initWithNibName:@"Blank" bundle:nil];
self.nextView=blankViewController.view;
nextView is a property that uses retain. Notice that blankViewController was not released. If it were released, this would cause a crash as the view doesn’t seem to keep a reference to the viewController. I want the view controller to stick around as long as a reference to the view is kept. What is the nicest way to fix this memory leak?
You need to store
blankViewControlleras an ivar ofself, or make it a (static) global variable.Then, when
selfis deallocated, you call[blankViewController release].You need to do this because there’s no other (documented) way to get the view controller from a view.