2

有人可以告诉我为什么会泄漏吗?我正在使用 CFRelease(),我认为它会发布 CFURLRef soundFileURLRef

调用函数“CFBridgingRetain”返回一个具有 +1 保留计数的核心基础对象
对象泄漏:在此执行路径中稍后未引用分配的对象,并且保留计数为 +1

  -(void) playGuitarNote:(NSString *)noteVal {

    AudioServicesDisposeSystemSoundID(soundId);
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef = CFBundleCopyResourceURL(mainBundle,CFBridgingRetain(noteVal), CFSTR("aiff"), NULL);
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundId);
    AudioServicesPlaySystemSound(soundId);
    CFRelease(soundFileURLRef);
    noteVal = nil;

}
4

2 回答 2

5

你不应该在CFBridgingRetain()那里打电话。您应该只使用__bridge演员表:

CFURLRef soundFileURLRef = CFBundleCopyResourceURL(mainBundle,(__bridge CFStringRef)noteVal, CFSTR("aiff"), NULL);

您没有更改 的所有权noteVal,您只是传递它并告诉编译器将其视为不同(但兼容)的类型。

于 2013-02-11T02:42:29.553 回答
2

您必须调用CFBridingRelease() 以减少保留计数。所以存储一个指向返回的对象的指针,CFBridgingRetain()当你不再需要它时释放它。

请参阅有关基础功能的文档

或者,您可以使用桥接而不是调用CFBridgingRetain()

于 2013-02-11T02:40:29.353 回答