Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / August 2005



Tip: Looking for answers? Try searching our database.

"when to release" confusion

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Keith Wiley - 16 Aug 2005 22:10 GMT
I asked about this a few days ago, and I thought the general answer was
that if I get an object as the return  value of a function, it has already
been autoreleased and I don't have to release it myself.  However, the
following code (which is from an email I posted earlier today on a
different topic), is taken from the docs and shows a returned object being
released when I am done with it:

int dropMethod = 0;
CFStringRef dropMethodKey = CFSTR("dropMethod");
CFNumberRef dropMethodVal = CFPreferencesCopyAppValue(dropMethodKey,
kCFPreferencesCurrentApplication);
if (dropMethodVal)
{
        if (!CFNumberGetValue(dropMethodVal, kCFNumberIntType,
&dropMethod))
                dropMethod = 0;
        CFRelease(dropMethodVal);
}
else dropMethod = 0;

So, how do I know if an object that has been returned from a function call
(message, whatever it's called) needs to be released when I'm
done using it?  In some cases, if I release the returned object of a
function, I get a warning suggesting I shouldn't be releasing it, but in
other cases, as shown above, a similar situation requires releasing such
an object.

I'm not detecting the pattern here.

Thanks.

________________________________________________________________________
Keith Wiley         kwiley@cs.unm.edu         http://www.unm.edu/~keithw

"Yet mark his perfect self-contentment, and hence learn his lesson,
that to be self-contented is to be vile and ignorant, and that to
aspire is better than to be blindly and impotently happy."
                                           --  Edwin A. Abbott, Flatland
________________________________________________________________________
Doug Brown - 16 Aug 2005 23:54 GMT
> CFNumberRef dropMethodVal = CFPreferencesCopyAppValue(dropMethodKey,
>
[quoted text clipped - 4 lines]
> other cases, as shown above, a similar situation requires releasing such
> an object.

The function you used, CFPreferencesCopyAppValue(), has Copy in the name.
That means you're responsible for releasing the returned object. The way
to tell is to look at the function name. Two of the words for which you're
looking are Copy and Create.

Hope this helps :)

Doug
Signature

Doug Brown - La Grande, Oregon

http://www.doogul.com/

Keith Wiley - 17 Aug 2005 01:25 GMT
>> CFNumberRef dropMethodVal = CFPreferencesCopyAppValue(dropMethodKey,
>>
[quoted text clipped - 9 lines]
> to tell is to look at the function name. Two of the words for which you're
> looking are Copy and Create.

Cool.  Thanks.

________________________________________________________________________
Keith Wiley         kwiley@cs.unm.edu         http://www.unm.edu/~keithw

"Yet mark his perfect self-contentment, and hence learn his lesson,
that to be self-contented is to be vile and ignorant, and that to
aspire is better than to be blindly and impotently happy."
                                           --  Edwin A. Abbott, Flatland
________________________________________________________________________
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.