how to solve memory leaks in AVAudioPlayer-iphonesdk - iphone

how to solve memory leaks in AVAudioPlayer-iphonesdk. here. i will give the my code.. memory leaks are in my code, how to solve it..
.h file
AVAudioPlayer *titlescreenaud;
.m file
titlescreenaud=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"TitleScreen_BgmAudio" ofType:#"mp3"]] error:NULL];//***Memory leaks on here......***
titlescreenaud.numberOfLoops=-1;
[titlescreenaud play];
After finish the sound
-(void)finish
{
[titlescreenaud stop];
[titlescreenaud release];
titlescreenaud=nil;
}
how to release the avaudioplayer please help me.......

titlescreenaud=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"TitleScreen_BgmAudio" ofType:#"mp3"]] error:NULL];//***Memory leaks on here......***
I assume Instruments is indicating that the memory leak is there. What it means is that something allocated on that line has not been released. The actual leak may be elsewhere and will most likely be because of a missing release.
Is your finish method really being called? Do you release titlescreenaud in dealloc?
There isn't enough code here to be able to specifically pinpoint the problem.

If you are allocating AVAudioPlayer object in ViewDidLoad than you will not face the problem. But if you are calling methods repeatatively and in which you have allocated the object than it is always going to allocate the memory for the same object. You can check the retainCount before releasing the memory.

Related

MPMoviePlayerController leaks memory on init?

I am using Xcode 4.3.1 with an ARC iOS (5.1) project and appear to have (according to instruments) a memory leak on the following:
player = [[MPMoviePlayerController alloc] init];
I have tried player as a property, ivar - with and without a video file, on a button click etc, etc yet this one line shows as a leak?
Am I missing something really obvious here? I have attached a screen of the leak.
Leak http://www.webelectrix.com/leak.jpg
Thanks,

iPhone APP delegate alloc warning

I see this output in allocations tool(one of too many warnings) Can anyone tell me what is going bad here? something with the UIImage and something very wrong with how I create/use navigation controller
And this is my category for imageview in APP_CATEGORIeS class import
#implementation UIImage(APP)
+(UIImage *) APP_IMAGE_BCKGROUND {
NSString *path = [[NSBundle mainBundle] pathForResource:#"bckPhone" ofType:#"png"];
return [UIImage imageWithContentsOfResolutionIndependentFile:path];
}
Looks like you have a UIImage in a past view that wasn't released that's causing dirty leak. Make sure your view transitions are smooth (allocations wise).
On a separate note, if you're setting two different anImageView images and it's retained in another class then Xcode and the compiler might not be catching it and warning you, however that'll do nasty things when the code is executed.

UIWebView memory issues

I am playing youtube videos on a UIWebView which appears as a modalViewController subview (flip transition). Everything works fine, even though the UIWebView is released, I still receive memory warnings after a few repeated selection of this modalViewController.
I have added my UIWebView programmatically inside ViewDidLoad. Inside viewDidDisappear I check for [UIWebView retainCount] and if greater than 1, perform the following steps:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[self.webView removeFromSuperview];
self.webView.delegate = nil;
self.webView = nil;
NSLog(#"[self.webView retainCount] %d", [self.webView retainCount]);
I am running my code on xCode 3.2.5, iOS 4.2.
Appreciate all you help.
I think you are approaching the memory management problem in the wrong way. Checking the retainCount is a valid debugging technique if you know what you are doing. It is not, however, a memory management tool. In your particular case, if the UIWebView is being displayed it will always have retain count > 1. The superview will have a retain on it thus making the "if" useless.
If the webView property is well defined (i.e. noatomic, retain) the statement:
self.webView = nil;
should release the webView. A common mistake is to initialize the property with:
self.webView = [[UIWebView alloc] init];
This is likely to introduce a leak if the webView is defined as "retain". The correct way is
self.webView = [[[UIWebView alloc] init] autorelease];
If you can't display your controller several times without running out of memory you have a memory leak. Use Instruments (Leaks in particular) to find hte objects what are note being released properly. This is a good tutorial.
Be careful in keeping your retains and releases balanced and check for leaks.
Your problem will be related to this:
Is it possible to prevent an NSURLRequest from caching data or remove cached data following a request?
Scroll down to my answer for an extension of the accepted answer - i had this problem for days and it's now resolved!

AVCaptureStillImageOutput outputSettings memory leak

I'm experiencing a wierd behavior in the new AVFoundation classes in the iPhone SDK.
I have a AVCaptureStillImageOutput for taking pictures, and I am setting its outputSettings to my liking. The code follows:
AVCaptureStillImageOutput *stillImageOutput = [[[AVCaptureStillImageOutput alloc] init] autorelease];
[stillImageOutput setOutputSettings:[NSDictionary dictionaryWithObject:AVVideoCodecJPEG forKey:AVVideoCodecKey]];
[self setStillImageOutput:stillImageOutput];
(stillImageOutput property is defined as "retain")
I stumbled upon a leak in leaks, with 100% of the leak fault on the setOutputSettings line. I believe that I confine to the memory management guidelines in the code attached, still it is leaking.
My solution was to
[self.stillImageOutput setOutputSettings:nil];
in the dealloc, just before
[self setStillImageOutput:nil];
The leak indeed stopped, but it looks weird. Shouldn't the releasing of stillImageOutput release its outputSettings property as well?
Anyway, if someone else runs into this, thought I should share my solution.
Cheers!
Oded.
Yes, the releasing of stillImageOutput should release it's outputSettings property as well. Either it's an Apple bug (should let them know, your use case is pretty simple) or remove your line, and see whether anything other than your class is hanging onto that stillImageOutput object (which is holding the outputSettings).

memory leak in device not in simulator

I have checked using instruments and not found any memory leaks.
when i check in device it shows memory leaks with responsible caller -[NSKeyedUnarchiver decodeObjectForKey:] and object is UIRoundedRectButton.
I still not using NSKeyedUnarchiver or any type of decoding. Is following code, is responsible for this memory leak ?
- (void)saveToFile:(NSString *)pinStr
{
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:#"smsbrain.plist"];
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
[data setObject:#"User" forKey:#"username"];
[data setObject:#"password" forKey:#"password"];
[data setObject:pinStr forKey:#"pin"];
[data writeToFile:path atomically:YES];
[data release];
}
Or i have checked through internet & also some post on stackoverflow but not find any better solution.
I am not setting outlet to nil in viewdidunload & in dealloc methods. is it create any problem?
my application hides background when this leaks occur. and on console i get warning "Memory level is not normal (20 %)"
So, what to do for the problem of invisible of background of tableviewcontroller. it is working fine on simulater. but what is problem in device that it sometime hide background.
please help me...
If you are not doing any NSCoding on yourself, the responsible caller is most likely a UIViewController that is decoding it's nib file. The memory warning you receive fits into this assumption.
When a memory warning is raised the standard implementation of UIViewController will release it's view, if is not visible (doesn't have a superview). As a result all subviews of that view will receive a release message and be dealloced, if no one retains them. But normally the viewController will at least retain some of it's view subviews trough it's IBOutlet properties. This is were viewDidUnload comes into play. It's called right after the view was unloaded (mostly due to a memory warning). This is the place were you really should release all retained subview of your view. That is every IBOutlet and erverything you created in viewDidLoad. There's really no need to hold them. They will all be recreated from the nib when needed.
In your dealloc method you have to release all retained properties and all retained ivars, that do not back a property. Please read the Memory Management Programming Guide
If this doesn't help. We would need your property declarations and the viewDidLoad, viewDidUnload, dealloc methods of the Controller in question.