我似乎无法解决的内存泄漏
所以分析器现在告诉我有内存泄漏。在下面的函数中,它说“分配到‘theAudio’的对象可能泄漏,
我认为它说的是事实,因为该应用程序运行良好几分钟,然后慢慢崩溃。
我尝试过“自动释放”,但它告诉我“对象发送自动释放太多次”。
抱歉,我是个害虫,但有人对此有什么想法吗?
-(void) playFile:(NSString*) nameOfFile { // plays audio file passed in by a string
fileLocation = nameOfFile;
NSString *path = [[NSBundle mainBundle] pathForResource:nameOfFile ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
[theAudio play];
[fileLocation release];
}
So analyzer is now telling me i have a memory leak. In the function below it says 'potential leak of an object allocated into 'theAudio'
I think it speaks the truth because the app works well for a few minutes then slowly crashes.
I've tried 'autorelease' but it tells me 'object sent autorelease too many times'.
Sorry to be a pest but does anybody have any ideas on this?
-(void) playFile:(NSString*) nameOfFile { // plays audio file passed in by a string
fileLocation = nameOfFile;
NSString *path = [[NSBundle mainBundle] pathForResource:nameOfFile ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
[theAudio play];
[fileLocation release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
还没有使用过这个,但您可能需要在播放器上保留一个
retain
(就像您所做的那样),然后在完成后release
它,例如,当您获得AVAudioPlayerDelegate
方法之一时(因此您需要实现播放器的“委托”。)Haven't used this, but you probably need to keep a
retain
on the player (as you do) but thenrelease
it when you're done with it, e.g., when you get one of theAVAudioPlayerDelegate
methods (so you need to implement the player's `delegate.)