CFBridgingRetain的静态分析器泄漏警告

时间:2013-02-11 02:29:49

标签: objective-c ios6 core-foundation

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

调用函数'CFBridgingRetain'返回一个具有+1保留计数的Core Foundation对象 泄漏的对象:此执行路径中未引用已分配的对象,并且保留计数为+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;

}

2 个答案:

答案 0 :(得分:5)

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

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

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

答案 1 :(得分:2)

您必须致电CFBridingRelease()以减少保留计数。 因此,存储指向CFBridgingRetain()返回的对象的指针,并在不再需要时释放它。

请参阅Documentation on Foundation Functions

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

相关问题