0
-(IBAction)play2;

{
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef =CFBundleCopyResourceURL(mainBundle, 
                                             (CFStringRef) @"Bear3", CFSTR ("wav"), NULL);

    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

This is giving me an error:

potential leak of an object allocated " CFBundleResourceURL
returns a Core Foundation object with a +1 retain count
0

5 Answers 5

2

AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID); - leak here, because create added to retain count

use AudioServicesDisposeSystemSoundID after playing sound

2

CFBundleCopyResourceURL contains copy so your retain count on soundFileURLRef is in fact 1. When you are done with it call CFRelease(soundFileURLRef) to decrement your retain count.

In addition to the error you're getting, SAKrisT's answer about calling AudioServicesDisposeSystemSoundID on the object you created with AudioServicesCreateSystemSoundID is also something to address.

3
  • Avoid discussing the actual retain count of an object (which is strictly an implementation detail) and instead think about objects in term of ownership.
    – dreamlax
    Nov 8, 2011 at 19:20
  • @dreamlax I was discussing the retain count because it's mentioned in the error that the question is about.
    – NJones
    Nov 9, 2011 at 18:13
  • read the error more carefully, it does not tell you the actual retain count, just the relative retain count in terms of your ownership.
    – dreamlax
    Nov 9, 2011 at 19:17
2

CFBundleCopyResourceURL creates a CFURLRef object that you own, so you need to relinquish ownership of this object at some point with CFRelease. Similarly you will need to balance your call to AudioServicesCreateSystemSoundID with another call to AudioServicesDisposeSystemSoundID.

For Core Foundation, functions that have the word Create or Copy in their name return an object that you own, so you must relinquish ownership of it when you are done with it. For more information about Core Foundation memory management, see the Core Foundation Memory Management Programming Guide.

Just to give you a hint, I would probably handle the memory management like this (although I haven't coded Objective-C for a while). This also assumes you want to keep the URL reference for whatever reason:

@interface MyClass
{
    CFURLRef soundFileURLRef;
    UInt32 soundID;
}

@end

@implementation MyClass

- (id) init
{
    self = [super init];
    if (!self) return nil;

    CFBundleRef mainBundle = CFBundleGetMainBundle();

    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, CFSTR("Bear3"), CFSTR("wav"));

    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);

    return self;
}

- (void) dealloc
{
    AudioServicesDisposeSystemSoundID(soundID);
    CFRelease(soundFileURLRef);
    [super dealloc];
}

- (IBAction) play2
{
    AudioServicesPlaySystemSound(soundID);
}
6
  • after i release that the sound will not play anymore
    – alfred
    Nov 8, 2011 at 19:45
  • 1
    Release it when you no longer need it, i.e. when your class is deallocating.
    – dreamlax
    Nov 8, 2011 at 20:29
  • Make both the soundFileURLRef and soundID into instance variables, then in your object's dealloc method, put CFRelease(soundFileURLRef); AudioServicesDisposeSystemSoundID(soundID);. (It may not be necessary to make the URL object an instance variable)
    – dreamlax
    Nov 8, 2011 at 22:29
  • how would those instance variables be am new to coding so i don't know much
    – alfred
    Nov 8, 2011 at 23:15
  • @alfred: I have edited my answer, hopefully this clarifies a few things.
    – dreamlax
    Nov 8, 2011 at 23:29
1

If you're not using ARC (available in xcode 4.2) then you need to release anything you alloc. add [alert release] after [alert show].

0
-1

Whenever you use the keyword 'alloc' , it means you are allocating some memory space for your object. Now if you don't release it yourself or autorelease it, then it shows 'memory leak'. It is not only about uialertview, but for every other objects also.
You may want to release the alertview object in dealloc() method, but still it will show memory leak as the memory is unused for a long time.
So , first you show the alert by [alert show], then you need the object anymore, so release it by [alert release];
Enjoy !! :)

1
  • 2
    -1: He can't release in dealloc as the scope is inside the pushAbout function. It's not that it shows memory leak - it is a memory leak - and it would not show as a leak if it was unused for a long time but eventually released. Reread the memory management guide.
    – jrtc27
    Nov 10, 2011 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.