Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 简单的例子,但我不';我不明白为什么没有泄漏_Iphone_Xcode_Memory Management - Fatal编程技术网

Iphone 简单的例子,但我不';我不明白为什么没有泄漏

Iphone 简单的例子,但我不';我不明白为什么没有泄漏,iphone,xcode,memory-management,Iphone,Xcode,Memory Management,如果我在这里释放shotSound,它将不会播放。同时没有泄漏 为什么仪器显示没有泄漏?如何释放shotSound?您可以试试 AVPlayer*shotSound=[AVPlayer-player-with-url:filePath] 注意:这将返回一个新播放器来播放给定URL引用的单个视听资源。因为当按下按钮时,您将需要一次又一次的声音,而不是每次在按钮中初始化它,您应该在类级别声明它,或者您可以在viewDidLoad方法中初始化它,或者您也可以这样做 - (IBAction) btnFi

如果我在这里释放shotSound,它将不会播放。同时没有泄漏

为什么仪器显示没有泄漏?如何释放shotSound?

您可以试试

AVPlayer*shotSound=[AVPlayer-player-with-url:filePath]


注意:这将返回一个新播放器来播放给定URL引用的单个视听资源。

因为当按下按钮时,您将需要一次又一次的声音,而不是每次在按钮中初始化它,您应该在类级别声明它,或者您可以在viewDidLoad方法中初始化它,或者您也可以这样做

- (IBAction) btnFire:(id)sender {

    NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath],   @"/gunShot.wav"];

    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];

    AVPlayer *shotSound = [[AVPlayer alloc] initWithURL:filePath];

    [shotSound play];
}
dealloc
中释放它。如果您只想在该操作中获得声音,则可以使用
AVAudioPlayer


有时仪器的泄漏无法检测到,在这种情况下,您可以使用构建和分析工具。另外,正如您所说,如果您释放了播放器对象,那么应用程序崩溃可能是因为它的音频仍在播放,而您释放了播放器(不确定)。

这种方式有任何缺点吗?如果您只想播放一个文件,我想这就足够了。当你这样做时,当自动释放池被释放时,玩家被释放。这是正确的答案
shotSound
应分配一次,然后在
dealloc
viewDidUnload
中释放。我添加了
viewDidUnload
,因为当视图不存在时,没有理由保留分配@Ravin的代码将在需要时再次初始化它。谢谢。您的意思是shotSound应该是ivar吗?如果是(ivar-->实例变量),则是:)
- (IBAction) btnFire:(id)sender {

 if(shotSound==nil)
{
        NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath],   @"/gunShot.wav"];

        NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];

       shotSound = [[AVPlayer alloc] initWithURL:filePath];
}

        [shotSound play];

}