什么变量是僵尸?

时间:2012-02-23 15:33:57

标签: objective-c crash runtime nszombie

我想检查变量是什么时候是僵尸&当它不是,我有这样的功能,如果它实际存在,y必须从超级层移除,有时它已被删除,但作为一个僵尸,它在这一点上崩溃。我该怎么做才能在运行时检查变量是否是僵尸?

 if (avPlayerLayer) {
         [avPlayerLayer removeFromSuperlayer];  
 }

我有这个代码来创建它:

if (!avPlayer) {
        avPlayer = [[AVPlayer alloc] initWithURL:movieURL];
    } else {
        [avPlayer replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithURL:movieURL]];
        avPlayer.rate = 0.0f;
    }
}
avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];

我应该这样做吗?:

if (!avPlayer) {
        avPlayer = [[AVPlayer alloc] initWithURL:movieURL];
    } else {
        avPlayer = nil;
        avPlayer = [[AVPlayer alloc] initWithURL:movieURL];
        avPlayer.rate = 0.0f;
    }
}
avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];

有任何帮助吗?提前谢谢你!

2 个答案:

答案 0 :(得分:1)

为什么不将变量设置为nil,然后检查nil。

答案 1 :(得分:0)

您的第一次实施似乎有一个简单的不一致---

if (!avPlayer) {

    // This sets avPlayer to a retained object reference (retainCount==1)
    avPlayer = [[AVPlayer alloc] initWithURL:movieURL]; 
} else {
    // This sets avPlayer to an autoReleased object reference 
    // which will die as soon as the memory pool is drained next. 
    // You should have retained it before setting, and your problem will be gone.
    [avPlayer replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithURL:movieURL]];
    avPlayer.rate = 0.0f;
}

我会用:

[avPlayer replaceCurrentItemWithPlayerItem:[[AVPlayerItem playerItemWithURL:movieURL] retain]];

B.T.W,您无法在运行时确定对象的“僵尸”,因为僵尸机制依赖于您无法在客户端计算机上设置的某些系统配置(环境变量等)。这是一个仅调试工具,而不是一个适当的开发技术。 Zombie是你程序中的一个bug。僵尸旨在帮助您找到并消除与内存相关的错误,否则这些错误很难跟踪。