Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-28-2011, 08:23 AM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 28
jlerma1 is on a distinguished road
Default Memory (Core Foundation/Objective-C) - Potential leak of an object on...

Hi there,

I am very new to Xcode dev, but guess what? I did build an entire application with some sounds and switching views and believe it or not, it has value. Everything runs beautiful and it looks great with no real issues except for this error:


"Memory (Core Foundation/Objective-C) - Potential leak of an object..."

I have attched 2 screenshots. The top image is from the window generated on the left side of Xcode (see I don't even know what thats' called yet) and the bottom one is my code from the .M file.

I have 4 sound files which are .wav's and pretty much under 5 seconds. My app never crashes.

So here are my questions, how do I get rid of these errors? I know this is memory management related. if anyone gives assistance, please be very specific like by telling me to go to the .m or the appdelegate and do so and so. Unfortunately, I need my hand held on this question. I am learning the coding parlance but not there yet.


My other question is this; is this error grounds fro rejection, even if the simulator says it generates with no issues?

I am close to being able to submit my app and I want everything to be as perfect as possible.

Thank you in advance to anyone who can walk me through this.

Much Obliged,

Jared
Attached Images
File Type: jpg memory leak.jpg (20.0 KB, 17 views)
jlerma1 is offline   Reply With Quote
Old 08-28-2011, 08:38 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

That screenshot is unreadable, can you just paste some of the code where you are getting that warning.
smithdale87 is offline   Reply With Quote
Old 08-28-2011, 09:45 AM   #3 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 28
jlerma1 is on a distinguished road
Default Unreadable Screenshot

Quote:
Originally Posted by smithdale87 View Post
That screenshot is unreadable, can you just paste some of the code where you are getting that warning.
You are right. Sorry about that and thank you for reviewing this so quickly.

//The error message shows up on 4 of the same lines listed below

"AudioServicesPlaySystemSound(soundID);"

The 2 part error message for all 4 of the same lines reads:

//Here is the main message

Potential leak of an object allocated on line 88 and stored into 'soundFileURLRef'

//here are the sub messages

Call to function 'CFBundleCopyResourceURL' returns a Core Foundation object with a +1 retain count (owning reference)

//AND

Object allocated on line 88 and stored into 'soundFileURLRef' is not referenced later in this execution path and has a retain count of +1 (object leaked)

//Below is the code in question

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

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

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

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

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

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

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

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

I hope this helps. I would provide a better screenshot but apparently the thread won't allow it.


Thanks for your help in advance.

Jared
jlerma1 is offline   Reply With Quote
Old 08-28-2011, 10:46 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by jlerma1 View Post
You are right. Sorry about that and thank you for reviewing this so quickly.

//The error message shows up on 4 of the same lines listed below

"AudioServicesPlaySystemSound(soundID);"

The 2 part error message for all 4 of the same lines reads:

//Here is the main message

Potential leak of an object allocated on line 88 and stored into 'soundFileURLRef'

//here are the sub messages

Call to function 'CFBundleCopyResourceURL' returns a Core Foundation object with a +1 retain count (owning reference)

//AND

Object allocated on line 88 and stored into 'soundFileURLRef' is not referenced later in this execution path and has a retain count of +1 (object leaked)

//Below is the code in question

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

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

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

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

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

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

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

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

I hope this helps. I would provide a better screenshot but apparently the thread won't allow it.


Thanks for your help in advance.

Jared
Functions that start with "CF" are Core Foundation functions. They are low-level C based functions.

They use memory management rules that are very similar to Cocoa memory management rules.

Do a search on "get rule" in the XCode help system and read the Memory Management Programming Guide for Core Foundation. The short summary is that methods with get in the name return non-owning refererences that you don't need to release, but methods with "create" or "copy" in the name return owning references, and you need to release them when you are done with them.

The base type of Core Foundation objects is a CFTypeRef. Generally, you call CFRelease() on a Core Foundation object to release it. Some objects have custom release methods, though, so you need to call the specific release method for that object.

In all your code above, you should copy your CFURLRef, use it to create a soundID, and then release the CFURLRef using CFRelease (because you use CFBundleCopyResourceURL, which contains the word copy)
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 08-28-2011, 12:34 PM   #5 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 28
jlerma1 is on a distinguished road
Default I am new to this so...

Hi Duncan,

As I mentioned in my email, I have no real idea of what I am doing. Would you be able to spell out the .h & .m code and any other thing I would need to visit? Do I need to another framework?

I am sorry to ask this but as I mentioned before, I need my hand held on this error. I appreciate your assistance.

Thanks,

J
jlerma1 is offline   Reply With Quote
Old 08-28-2011, 01:10 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by jlerma1 View Post
Hi Duncan,

As I mentioned in my email, I have no real idea of what I am doing. Would you be able to spell out the .h & .m code and any other thing I would need to visit? Do I need to another framework?

I am sorry to ask this but as I mentioned before, I need my hand held on this error. I appreciate your assistance.

Thanks,

J

Sigh,

Did you even try to understand what I posted?

I'll show you what to do for one of your methods. You need to do the same thing for all of them. That will be up to you to figure out.

Code:
-(IBAction)Sound1 {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Sound1", CFSTR ("wav"), NULL);

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

I added the call to CFRelease in bold.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 08-28-2011, 01:40 PM   #7 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 28
jlerma1 is on a distinguished road
Default Thank You Duncan!

Quote:
Originally Posted by Duncan C View Post
Sigh,

Did you even try to understand what I posted?

I'll show you what to do for one of your methods. You need to do the same thing for all of them. That will be up to you to figure out.

Code:
-(IBAction)Sound1 {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Sound1", CFSTR ("wav"), NULL);

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

I added the call to CFRelease in bold.

Duncan,

It worked! I did read your post very carefully but there are pieces that were just no connecting. But your code put everything together and in the long run all the pieces will start to fit to better. Your guidance has helped me to take another step and I thank you for that,

Just a few months ago, I have never touched a Mac and now I am a few days away from submitting my app which has great content and a few nice effects that I have put in. Just so you know, i do read the Apple Documentation but I am not good enough to understand some things programmatically.

I really appreciate your assistance with this.

Thanks,

Jared
jlerma1 is offline   Reply With Quote
Old 08-28-2011, 01:49 PM   #8 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by jlerma1 View Post
Duncan,

It worked! I did read your post very carefully but there are pieces that were just no connecting. But your code put everything together and in the long run all the pieces will start to fit to better. Your guidance has helped me to take another step and I thank you for that,

Just a few months ago, I have never touched a Mac and now I am a few days away from submitting my app which has great content and a few nice effects that I have put in. Just so you know, i do read the Apple Documentation but I am not good enough to understand some things programmatically.

I really appreciate your assistance with this.

Thanks,

Jared
Core Foundation is harder to use than UIKit, for sure. NSObject memory management is pretty straightforward.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 05-16-2012, 11:42 AM   #9 (permalink)
New User
 
Join Date: Jun 2010
Posts: 7
macboy is on a distinguished road
Default Thanks for the help!

I was also experiencing the same issue with the memory leak when analyzing the code. I am using xCode 4.3.2.

This one line fixed the issue:
CFRelease(soundFileURLRef);

I think it common to overlook this type of error as just Building and Running does not show this as an error. I have learned to use Analyze to check every project.
macboy is offline   Reply With Quote
Reply

Bookmarks

Tags
potential leak of object

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 405
7 members and 398 guests
JackReidy, jeroenkeij, Leslie80, Sami Gh, Wikiboo, Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:46 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0