简体   繁体   中英

Static Analyzer Leak Warnings for CFBridgingRetain

Can someone tell me why this is leaking? I am using CFRelease(), which I thought releases CFURLRef soundFileURLRef

Call to function 'CFBridgingRetain' returns a Core Foundation object with a +1 retain count
Object leaked: allocated object is not referenced later in this execution path and has a retain count of +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;

}

You shouldn't be calling CFBridgingRetain() there. You should just use a __bridge cast:

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

You are not changing the ownership of noteVal , you're just passing it and telling the compiler to treat it as a different (but compatible) type.

You have to call CFBridingRelease() to decrease the retain count. So store a pointer to the object returned by CFBridgingRetain() and release it when you no longer need it.

See the Documentation on Foundation Functions

Alternatively you could use a bridge cast instead of calling CFBridgingRetain()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM