已更新
为什么 NSData dataWithContentsOfFile 行在 Instruments 中显示泄漏?
我正在使用 ARC。部署目标是 iOS 5.0
@autoreleasepool
{
AudioPlayerAV *context = [userInfo valueForKey:@"self"];
NSString *filepath = [userInfo valueForKey:@"filepath"];
[context.player stop];
//check if file is there fetch player from dict
AVAudioPlayer *_player = nil;
NSError *error = nil;
NSData *filedata = [NSData dataWithContentsOfFile:filepath];
_player = [[AVAudioPlayer alloc]initWithData:filedata error:&error];
context.player = _player;
NSLog(@"loadAndPlay error : %@",[error description]);
context.player.numberOfLoops = (context.loop)?-1:0;
context.player.volume = context.volume;
[context.player play];
}
最佳答案
有时候,仪器会指向错误的线路,我认为是this泄漏 AVAudioPlayer .
来自:Leak from NSURL and AVAudioPlayer using ARC
Looks to be a leak in Apple's code... I tried using both
-[AVAudioPlayer initWithData:error:] and -[AVAudioPlayer initWithContentsOfURL:error:]
In the first case, the allocated AVAudioPlayer instance retains the passed in NSData. In the second, the passed in NSURL is retained.
You can see the AVAudioPlayer object then creates a C++ object AVAudioPlayerCpp, which retains the NSData again
Later, when the AVAudioPlayer object is released, the NSData is released, but there's never a release call from the associated AVAudioPlayerCpp... (You can tell from the attached image)
查看一下,answer 中附有一些仪器截图.
关于iphone - NSData dataWithContentsOfFile 仪器 100% 泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14084796/