I started to learn using Instrument, but I cannot figure it out.
After I start my application, the UI shows up, I do nothing and after few seconds I can see memory leak detected:
When I have a look at the second leak I can see the following stack:
When I double click on the cell related to my code I can see that it is pointing to the following line of code:
[window addSubview:newPostUIViewController.view];
from the method:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//creating view controller
newPostUIViewController = [[NewPostUIViewController alloc] initWithNibName:#"NewPostView" bundle:nil];
newPostUIViewController.title = #"Post it!";
[window addSubview:newPostUIViewController.view];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
I wonder, how this can be a reason of a leak? I release newPostUIViewController in the dealloc method of PostItAppDelegate class.
Any ideas how this could be explained?
You did not provide an autorelease or release to balance your init. Just in case you haven't read through it already, have a look at the memory management guide is a great help.
Looking at link text allows to say that this is Simulator problem, not the code problem.
Related
Please see below sample . Every thing works but memory is not released. Here MYTest is not getting released and memory seems to be increasing when watched in instruments tool
- (IBAction)methodXYZ:(id)sender
{
MYTest * myTest = [[MYTest alloc]initWithNibName:#"myNib" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: myTest];
[self presentModalViewController:navigationController animated:YES];
}
}
and in myTest when finished i call
[self dismissModalViewControllerAnimated:YES];
i also tried setting delegate and dismissing from parent , but that also doesnt solve the issue ..
Any help will be greatly appreciated.
Thanks
mia
If ARC is enabled (be sure it is) you dont need to worry about it, the system will dealloc the memory when it's necessary.
In the other hand, I invite you to take a look at the classic way (non ARC), its useful sometimes if you want to have a control of the memory like it seems you want.
Maybe I've been looking at this for too long ;) My app has a NavigationController and several ViewControllers. From one of the ViewControllers two levels down (mainViewController), loaded from the rootViewController, I have the code below. After the PushViewController to the dataViewController and back (e.g. back Button pressed), the app crashes.
The dataViewController loads just fine, but when the back button of the navigationController is tapped, the App crashes with Object Exception. If I remove:
[dataViewController release];
the app works fine. It's strange because the dataViewController is init'ed in the same method.
Any ideas?
- (void) locationPage
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"NotifyRemoveMap" object:nil];
MyAppDelegate *app = [[UIApplication sharedApplication] delegate];
UINavigationController *navigation = app.navigationCantroller;
[navigation popToRootViewControllerAnimated:NO];
DataViewController *dataViewController = [[DataViewController alloc] initWithNibName:#"DataView" bundle:nil];
[dataViewController setCategoryId:category];
MyLanguage *lang = app.lang;
Mylocation *location = [lang locationForCategoryId:category];
dataViewController.title = location.name;
NSArray *locationArray = [lang locations];
dataViewController.locations = locationArray;
[navigation pushViewController:dataViewController animated:YES];
[dataViewController release]; // With this removed, app doesn't crash
}
Haven't even read your post. If it's Exec-Bad-Access, I have 2 words for you:
Enable NSZombies.
Follow this link: (it explains everything you need to know to fix any bad access issue)
Phone Memory Debug with NSZombie and Instruments
Cheers!
The problem probably arises when the dataViewController gets popped and you try to access something on it - it is already released then. You might check the console for more details - better yet, run in debug mode (debug configuration and running with debugger).
You can edit your question to show some code that is run with the back button.
You talk about releasing dataViewController but your code says detailsViewController. Did you copy and paste incorrectly or is that the mistake?
You should consider not to use app.navigationController but self.navigationController. Cleaner design. Less dependencies on the app delegate, which too often is used as a frankensteinobject that knows too much.
Never thought I'd get an error in this file when I compiled, but after running my app in debug mode, The app stops and highlights the line
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
in my main.m file as a breakpoint. When I run in normal mode, the app just shows a black screen. What does this mean?
(Update)
Okay, nothing has worked so far. Here is the code that is failing:
#import <UIKit/UIKit.h>
int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
The stack says
Pending breakpoint 1 - ""main.m":31" resolved
The weird thing is, I removed the aforementioned view controller that preceded this problem, there are still no warning or errors and I the application still either halts in debug mode or returns a black screen and freezes in normal running mode.
Here is a drop containing two screenshots of my view and window arrangements in my two NIBs in IB. You can also download my full project to browse the code, if you wish. The project is named "iHouse.zip"
http://drop.io/6lhubkb
Otherwise, any other specific code anyone would like to see, I'm happy to paste it here on request. Otherwise, I'm stumped.
Thanks.
Solved....sort of. The "31" in the stack was oddly referring to a random line in the commented out apple disclaimer in the beginning of main.m. I removed this and that problem went away. Still not sure why it suddenly became a problem in the first place.
I compared the code side-by-side with another app and found that I had commented out a critical part of applicationDidFinishLoading in the AppDelegate as #IWasRobbed had suggested.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
[window addSubview:splitViewController.view];
[window makeKeyAndVisible];
}
else
{
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
Because I was not ready to make this app universal, I commented this part out. In doing so, I took out the crucial piece to tell the app what the heck to load in the first place
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
Thanks everyone for your help. Took all day to find a silly error on my part, but you live and you learn I guess.
It means you probably forgot to hook up a window or view to load after the application launches. Unless you modified the main.m file (which you shouldn't need to), it should be just fine.
Maybe someone can help me with this strange thing:
If a user clicks on a button, a new UITableView is pushed to the navigation controller. This new view is doing some database querying which takes some time. Therefore I wanted to do the loading in background.
What works WITHOUT leaking memory (but freezes the screen until everything is done):
WorkController *tmp=[[WorkController alloc] initWithStyle:UITableViewStyleGrouped];
self.workController=tmp;
[tmp release];
[self.workController loadList]; // Does the DB Query
[self.workController pushViewController:self.workController animated:YES];
Now I tried to do this:
// Show Wait indicator
....
WorkController *tmp=[[WorkController alloc] initWithStyle:UITableViewStyleGrouped];
self.workController=tmp;
[tmp release];
[self performSelectorInBackground:#selector(getController) withObject:nil];
}
-(void) getController {
[self.workController loadList]; // Does the DB Query
[self.navigationController pushViewController:self.workController animated:YES];
}
This also works but is leaking memory and I don't know why !
Can you help ?
By the way - is it possible for an App to get into AppStore with a small memory leak ? Or will this be checked first of all ?
Thanks in advance !
No, small memory leaks will not (most likely) you application to be rejected from appstore.
In your example as you run your method in separate thread you should create and dispose NSAutoreleasePool object for that thread to handle autoreleased objects. Following changes to getController method should do the trick:
-(void) getController {
NSAutoreleasedPool *pool = [[NSAutoreleasedPool alloc] init];
[self.workController loadList]; // Does the DB Query
[self.navigationController pushViewController:self.workController animated:YES];
[pool release];
}
For more details see Autorelease Pools section in memory management guide. Relevant quote from there:
If you spawn a secondary thread, you
must create your own autorelease pool
as soon as the thread begins
executing; otherwise, you will leak
objects. (See “Autorelease Pools and
Threads” for details.)
Btw, you're calling pushViewController: from a background thread. This is bad.
You should only do things to the UI - like pushing view controllers and changing UI items - from the main thread. If you don't, things break.
See the Cocoa Fundamentals Guide section titled "Are the Cocoa Frameworks Thread Safe?": it says "All UIKit objects should be used on the main thread only."
uiimagepickerview controller creating memory leaks in iphone - why?
Try to implement ui image picker view controller in your application & debug it.
You will find memory leaks in your application.
Why ui image picker view controller creates memory leaks.
-(void)addPhotos:(id)sender
{
if(imagePickerController==nil){
imagePickerController=[[UIImagePickerController alloc]init];
imagePickerController.delegate=self;
imagePickerController.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePickerController.allowsImageEditing=YES;
imagePickerController.navigationBar.barStyle=UIBarStyleBlackOpaque;
}
[self.navigationController presentModalViewController:imagePickerController animated:YES];
}
dealloc of my view controller.
- (void)dealloc {
if(PhotoDateArray!=nil)[PhotoDateArray release];
if(imagePickerController!=nil) [imagePickerController release];
if(objDetail!=nil) [objDetail release];
if(Picimage!=nil) [Picimage release];
if(mySavePhotoController!=nil) [mySavePhotoController release];
if(LoadingAlert!=nil);
[super dealloc];
}
Video link explaining how I am getting the memory leak in it..
http://www.yourfilelink.com/get.php?fid=508534
Even though you have the nil check, it's still possible to leak memory. I think what is happening here is that you are calling alloc / init multiple times, but only releasing once. My guess it that addPhoto: is wired up to some button click, dealloc would only be called once when the delegate is trying to destroy. This creates a situation like this:
button click
alloc / init
button click
alloc / init (memory leak on first alloc'd picker)
close window
dealloc (free second alloc'd picker)
A better way might be the way Apple does it in the PhotoLocations and iPhoneCoreDataRecipes examples:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
Then listen for the didFinishPickingImage and imagePickerControllerDidCancel messages to your delegate and a call to [self dismissModalViewControllerAnimated:YES]; in both places should suffice.
I dont know about the rest of the code, but do you ever have a release?
[imagePickerController release]
UIImagePickerController loads and initializes PhotoLibrary.framework the first time it is shown. This memory won't be reclaimed until your application is closed.
(the code you posted doesn't appear to have leaks as-is, but that doesn't mean it won't interact with the rest of your application in a way that causes them)
I can explain this because I was having the same problem.
Don't test memory on the simulator!
If you test the apple code on a device the memory problem disappears.
I was having a memory alloc leak which I found in Instruments. All I was doing was opening and closing the image picker (open/cancel) and using Apple code, my code and other people's code, just like yours above.
All were showing the allocation going up and up each time, as if the picker was not being released. If you tried to release it, it would crash (over released).
Then I found a really helpful web page which basically stated:
"This doesn't happen when testing on the device"
So I switched from the simulator and ran the tests on the device. Lo & behold there was no allocation increase and it behaved normally.
This however is totally evil and now we can place no trust in the simulator to do a reliable job.
I want to add this to save people, the time, pain and bewilderment of wondering wtf is going on!