我做的第一件事是将日志记录到所有 AVCam 文件的 dealloc 方法中。我很快发现 AVCamCaptureManager 和 AVCamRecorder 没有在 AVCamViewController 被释放时被释放。我检查了保留和释放调用,它们似乎是平衡的,所以我在 [captureManager 释放] 上放置了一个断点,发现它在释放后的 retainCount 为 2(因此没有调用 AVCamCaptureManager 释放)。
接下来,我逐步完成了捕获管理器的创建过程,发现在调用 init 方法后立即保留计数为 3。
逐步执行 init 方法并检查每一行的保留计数,我发现以下两行都在增加保留计数:
[self setDeviceConnectedObserver=[notificationCenter addObserverForName:AVCaptureDeviceWasConnectedNotification object:nil queue:nil usingBlock:deviceConnectedBlock]];
[self setDeviceDisconnectedObserver=[notificationCenter addObserverForName:AVCaptureDeviceWasDisconnectedNotification object:nil queue:nil usingBlock:deviceDisconnectedBlock]];
通过查看,我发现 removeObserver 对应项位于 AVCamCaptureManager 的 dealloc 方法中(未被调用),因此保留计数从未降至 0。
为了修复它,我创建了一个新的公共 removeObservers 方法:
-(void)removeObservers {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:[self deviceConnectedObserver]];
[notificationCenter removeObserver:[self deviceConnectedObserver]];
}
并从 AVCamCaptureManager dealloc 方法中取出相同的行。
调用 [captureManager removeObservers];然后调用 [captureManager release];在 AVCamViewController dealloc 方法中成功将保留计数降至 0。
使用 Activity Monitor 进行测试后,现在 mediaserverd 进程的嗡嗡声只有 5-17Mb,并且崩溃停止了!
希望这可以帮助其他遇到此问题的人!