Reputation: 23510
When writing this common statements :
UIViewController* viewController = [[UIViewController alloc] initWithNibName:@"myXIB" bundle:nil];
/* do some things with the viewController */
[self.view addSubview:viewController.view];
I'm wondering if there is a possible memory leak here, because :
So I see a memory leak here where viewController is never released.
Could you give me your expert knowledge about that ?
Note : Instruments does not find any memory leak there.
Upvotes: 2
Views: 223
Reputation: 1
Tableview didselrowatindex method is a common place where such memory leaks occur.
One way to do away with memory leaks is to make sure you only create one instance of the ViewController and then reuse it.
Furthermore this technique doesn't just apply to creating and showing viewcontrollers but anything. If you are creating anything in code such as uilabels, etc, make sure that you are not creating multiple instances.
/* code */
// in the interface create an ivar ... or a property if needed
UIViewController *childViewController;
/// then later in your code , tableview didSelectRow, etc
// don't create multiple instances of the VC, rather, check to see if we can reuse one
if (! childViewController)
{
childViewController = [[UIViewController alloc] init];
}
// do some things with the viewController
childViewController.someProperty = someValue;
[self.view addSubview:childViewController.view];
Upvotes: 0
Reputation: 26390
If you want view as a sub view I think its only better to have the view controller as an instance variable of the class and release
it in dealloc
UPDATE
Taken from a previous SO question
Instrument's leak detection works by conservatively scanning memory, looking for pointers and building a graph of connections between allocations. If it finds any pointer(s) to an object in memory that can be reached from a global variable or stack variable, then that object cannot be considered leaked.
Instruments does not know the layout or context of a pointer. If you were to malloc(1024) and there happened to be a bunch of pointers in that [recycled] chunk of memory, those would count even though you will never treat those pointers as real references again.
So, no, Leaks can never be 100% accurate. As well, there are far more many ways to leak memory than an actual leak.
Upvotes: 1
Reputation: 2230
Yes, memory will leak, you should always release memory allocated by yourself.
When you add [viewController release]
the viewController is dealloced immediately and then your app crashes, because it's trying to access dealloced memory. To prevent it, you should include your viewController as instance variable of your class.
Upvotes: 1
Reputation: 1837
There is definitely a leak here as you allocate a new UIViewController but never release it (it is not added to the autorelease pool as you do an allocation). You have to keep a reference on this viewController object and release it later on.
As you said, if you call right after [viewController release], it does crash your application because you have just added its internal view to self.view but the reference counter of the controller to which this view (viewController) belongs has not been incremented.
Upvotes: 0