How to convert code to properly release memory
Posted
by BankStrong
on Stack Overflow
See other posts from Stack Overflow
or by BankStrong
Published on 2010-04-19T10:17:58Z
Indexed on
2010/04/19
10:23 UTC
Read the original article
Hit count: 262
objective-c
I've taken over a code base that has subtle flaws - audio player goes mute, unlogged crashes, odd behavior, etc.
I found a way to provoke one instance of the problem and tracked it to this code snippet:
NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:[[soundsToPlay objectAtIndex:count] description]
ofType:@"mp3"]];
self.audioPlayer = nil;
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:soundURL error:nil];
self.audioPlayer.delegate = self;
AudioSessionSetActive(YES);
[audioPlayer play];
When I comment out the 2nd line (nil) and add a release to the end, this problem stops.
[self.audioPlayer release];
- Where do I go from here?
- Nils are used in a similar fashion throughout the code (and may cause similar problems) - is there a safe way to remove them?
- I'm new to memory management - how can I discern proper nil usage from bad nil usage?
© Stack Overflow or respective owner