开发者

Should I release NSBundle in sound?

Should I release NSBundle in the below code or not? NSURL should also be release or not? I am confused.

NSBundle  *mainBundle = [NSBundle mainBundle];
NSError   *error开发者_开发百科;
NSURL     *audioURL = [NSURL fileURLWithPath:[mainBundle pathForResource:@"count_in" ofType: @"mp3"]];
AVAudioPlayer *player1 = [(AVAudioPlayer*) [AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
self.player =   player1;
[self.player play];
[player1 release];


You should not release NSBundle and NSURL instances because you haven't alloced these.

From the apple Documentation.

You take ownership of an object if you create it using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message.

You use release or autorelease to relinquish ownership of an object. autorelease just means “send a release message in the future” (specifically: when the used autorelease pool receives a drain message—to understand when this will be, see “Autorelease Pools”).

I would highly recommend you to clear your memory management concept.

Read the apple article on Memory Management Rules

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜