Memory leaks in iphone App - iphone

Am all most done with my App.When am running the App on Simulator it works fine but when i run on the device for some time the debugger showing a message as "Program received signal 0".I tested the app with the help of instruments leaks it showing leaks.But am unable to figure out exactly where was the leak.Am releasing every object which am allocating.
For example am providing little part of my code:
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker=[[UIImagePickerController alloc] init];
picker.delegate=self;
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
// NSFileHandle *fileHandle = [[NSFileHandle alloc]initWithFileDescript
}
In the above code leaks are showing at the UIImagePickerController *picker=[[UIImagePickerController alloc] init]; but am releasing it.
Can some one provide me the detail information abt the leaks and how to fix them .Thanks in advance.

In this line of code:
[self presentModalViewController:picker animated:YES];
self (the parent controller) is actually retaining your picker and won't release it until the parent controller explicitly dismisses it. As long as your parent controller are dismissing this picker at some point in your code you'll be fine.

Related

Crash / Memory Warning when taking a picture - iPhone

I am getting a memory warning and the app is crashing during the time when a photo is taken with the iPhone. Not sure how to address this because the code that opens the camera works - and the code after a picture is captured works, but the error is somewhere in the camera taking a picture section of the app...
I'm starting the camera like this:
imagePicker = [[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
}
else
{
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
} else if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
I am getting "Received memory warning." right before Recorder_SourceStarted and AVCaptureDeviceDidStartRunningNotification.
I then get Recorder_DidStartPreviewing, Recorder_FlashStateChanged, Recorder_DidStartFocusOperation, Recorder_DidCompleteFocusOperation, Recorder_FlashStateChanged, Recorder_WillCapturePhoto, Recorder_DidCapturePhoto, and Recorder_PhotoStillImageSampleBufferReady before the app crashes.
Recorder_PhotoStillImageSampleBufferReady is the last notification I am getting before the app crashes. It doesn't make it to _UIImagePickerControllerUserDidCaptureItem at which point the app starts running code I wrote again.
Sometimes it doesn't crash... I get the memory warning and the app continues working as it should, but the potential is there for a crash because of this memory warning and I'm getting crashes in testing so I'd like to figure out what is causing this and fix it.
Any help would be great! Thanks!
I was alloc'ing tableViews in the app (not dealloc'ing because it's using ARC) and apparently after the calls to alloc tableViews had been made, trying to capture a picture would cause a memory crash. I changed it to set the .hidden property on the tableviews on and off when users needed to see the table instead of alloc'ing each time a tableview was requested and now the app never crashes when a picture is taken. Sometimes there is still a memory warning, but never a crash, and often no memory warning at all when I take a picture.

UIImagePickerController does not release memory it occupies

I saw this thing on using instruments for my app. When I profile my app, the initial memory occupied is 563 KB which is before UIImagePickerController pops up. There is one button on the first viewController which makes the UIImagePickerController appear.
As soon as UIImagePickerController appears, memory occupied goes upto 1.6 - 1.7 MB. If I select any image or cancel the UIImagePickerController, the memory occupied is still 1.6 - 1.7 MB which I believe should be 563 KB(or may be few KB's more).
Please see the below code I have used :
- (IBAction)chooseButtonPressed:(id)sender
{
UIImagePickerController *pickerController = [[UIImagePickerController new]autorelease];
[pickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[pickerController setDelegate:self];
}
Why is the memory not being released?
We can't add images to comments so i'm putting this as an answer. The Live Bytes are always less than the Overall Bytes except till the first time a memory is deallocated. This can be seen from the image below.
I don't think there's anything wrong with your deallocation. I think you're just looking at the wrong values!
EDIT- I Think the problem might be somewhere else. To see the values I was seeing, you need to make a little change. As shown in the image below, you need to uncheck the option Only track active allocations to see the values you're looking for. If you still see 7.41 MB in Active allocations, then the problem is something else.
Since you have given it autorelease option it will get added to the autorelease pool ... see what the documentation say..
The Application Kit creates an autorelease pool on the main thread at
the beginning of every cycle of the event loop, and drains it at the
end, thereby releasing any autoreleased objects generated while
processing an event.
you can always release the picker in the delegate call like this..
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
...
...
[picker release];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
....
....
[picker release];
}
try this
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
Did you set delegate to nil?
For more information you can refer to UIImagePickerConrtoller class reference
[picker release];
picker.delegate = nil ;
Hope this helps you.

Please assist with modalViewController crash

My app is crashing when I dismiss a ModalViewController via:
[self.parentViewController dismissModalViewControllerAnimated:YES];
This modal view-controller ("MVC") is presented when a user clicks on one of the cells of a UINavigationController ("NavRoot") - here's the code for that:
MVC *modalView = [[MVC alloc] initWithNibName:#"MVC" bundle:nil];
[self.navigationController presentModalViewController: modalView animated:YES];
[modalView release];
The "modalView" which is loaded contains only 2 objects: a UIWebView object and a "DONE" button, which when clicked-on does the dissmissing via:
[self.parentViewController dismissModalViewControllerAnimated:YES];
Except when I click on "DONE" - the app crashes.
When I run Instruments with NSZombies I do see the retain count reaches -1 but I can't tell what's causing this over-release.
The only thing I found which solves the problem is to either add a "[modalView retain]" statement in "NavRoot" - which is the viewController doing the presenting of modalView:
MVC *modalView = [[MVC alloc] initWithNibName:#"MVC" bundle:nil];
[self.navigationController presentModalViewController: modalView animated:YES];
[modalView retain]; // <<== new 'retain' statement
[modalView release];
or just simply never releasing modalView in the first place:
MVC *modalView = [[MVC alloc] initWithNibName:#"MVC" bundle:nil];
[self.navigationController presentModalViewController: modalView animated:YES];
// commenting out the 'release':
// [modalView release];
Both of these options throw flags when I run "Analyze" ("Potential leak of an object allocated on line 34"...) but they do fix the problem.
Still, I worry about this causing the app to be rejected by Apple from the App Store.
Any ideas on what may be causing the over-release? Or how I might further try to isolate / identify the problem?
attaching an image of Instruments/Zombies report:
Are u using iOS 5? I had the same problem when I switched an app from ios4 to 5.
ParentViewController is now called presentingViewController
What you can do though is in your modal view just call [self dismissModalViewController] and it should dismiss itself. I'm not 100% about that and can't check as I'm not near my mac, but I recall reading it in the docs,
If you do
[self.navigationController presentModalViewController: modalView animated:YES];
Then you should dismiss it like
[self.navigationController dismissModalViewControllerAnimated:YES];
Rather than
[self.parentViewController dismissModalViewControllerAnimated:YES];
Where are you trying to dismiss the view from? The actual modalView or the parentView? It seems to me that you are trying to dismiss a modal view that has already been dismissed and subsequently released.
To dismiss a modalViewController I simply just do: [self dismissModalViewControllerAnimated:YES];.
[self dismissModalViewControllerAnimated:YES] does not work on iOS 5.
I have built a category that add presentingViewController on iOS 4. (It disables itself on iOS 5.)
Just include 2 files, and it works seamlessly.
Please see backward-modal.
I hope this benefits you as much as it does to me; It makes your code more clean!

navigationController crash

I have a question about iPhone develop
CarDetailDetail *myview = [[[CarDetailDetail alloc] init] autorelease];
myview.detailMaintainID = self.detailMaintainID;
[[self navigationController] pushViewController:myview animated:YES];
this is work fine in iPhone 4 (iOS 4.3) and iPhone 3gs (iOS 5.X)
but iPod touch (iOS 4.2) will crash when I pop back one or two times,
memory warning will appear and has bad access error
but when I not release *myview (remove autorelease keyword) , iPod works fine...
I don't know why , some one can help me? thanks
Experiment with the YES/NO flag on the pop animation
If you find that you are getting no crash on your problem device with NO pop animation it probably indicates a timing issue of some sort.
In other words a block or web thread is trying to hit a view controller which has been deallocated already.
post your your console logs in the question too for more help.
try doing this
CarDetailDetail *myview = [[CarDetailDetail alloc] initWithNibName:<name of view contrlle> bundle:nil];
//Remove this bit of code myview.detailMaintainID = self.detailMaintainID;
[[self navigationController] pushViewController:myview animated:YES];
[myview release];

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!