tracing a memory leak with Instruments in iPhone app - iphone

I am having problem finding a memory leak with Instruments. Usually it helps me a lot and I am able to find the leak, but in this case I'm lost.
I am creating a view controller that controls a views loaded from NIB file. The view has Map View with "Show user location" on true. Once user location is found I use MKReverseGeocoder to get the location data. The leak is always present when I load this view controller and MapKit finds user location. I figured out that MKReverseGeocoder isn't problem here, since I get the same leak with or without the MKReverseGeocoder.
When I load this view Instruments "leaks" report a memory leak. See the screenshot on the image:
This is how I initialize my controller:
AddPlaceViewController *addPlaceVC = [[AddPlaceViewController alloc] initWithNibName:#"AddPlaceViewController" bundle:[NSBundle mainBundle]];
addPlaceVC.delegate = self;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addPlaceVC];
self.placeController = navigationController;
[self presentModalViewController:self.placeController animated:YES];
[addPlaceVC release];
[navigationController release];
This is all on the iPhone Simulator 4 and targeted OS 3.2.
Is this actually leak or what I am facing here?

Since the responsible library is "Foundation" and not your code, there is nothing you can do about it (except report it to Apple).

Related

Memory leak issue in navigation controller

I am doing memory leak testing. The tool indicates a leak in line 2 of the code below. What is the reason?
DailySales *ds = [[DailySales alloc] initWithNibName:#"DailySales" bundle:nil];
[self.navigationController pushViewController:ds animated:YES];
[ds release];
The code looks fine. Are you running in the simulator or on the device? Running Instruments on the simulator can produce false leak reports.
Objects loaded from the NIB are leaking. Check your outlets and see if they are carefully released in dealloc.

Leaks in navigationcontroler when puching it

i have found leaks in the below code at particular area, means when i push my view from other sides, There is not any leak i found but only this push give me 100%leak.
actionsListing *View = [[actionsListing alloc] init];
[self.navigationController pushViewController:View animated:YES];
[View release];
What will i do thanks.
That code is fine - you're doing everything correctly.
Are you testing in the simulator or on a device? If it's the simualtor, don't. The simulator has a different memory model and reports leaks that aren't really there; always test for leaks on a device :)
If you still get leaks, you're retaining the view controller somewhere else by mistake - that code is definitely correct.

Strange Exc Bad Access when using Init, PushViewController, Release. Anything wrong with this code?

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.

How UIWindow#addSubview can make memory leak?

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.

uiimagepickerview controller creating memory leaks in iphone - why?

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!