On Tue, Dec 30, 2008 at 4:14 PM, Steve Cronin <steve_cro...@mac.com> wrote:
> Michael - So does the revised code below fix the leak?
>
> After a bit more studying of the documentation, I offer these revised 'more
> Cocoa-friendly' updates:
>
> The driving issue is a helper app which wants to access prefs written by the
> 'motherApp'.
>
> - (NSDictionary *) prefDictionary {
>        CFStringRef appBundleID = (CFStringRef)motherAppBundleID;
>        //Core Foundation 'create rule' sez I own this -> I must dispose of
> it  (because 'copy' or 'create' in CF method name)
>        CFDictionaryRef prefs = CFPreferencesCopyMultiple(NULL,  appBundleID,
>  kCFPreferencesCurrentUser,  kCFPreferencesCurrentHost);
>        //cast it into an auto-released Cocoa object
>        NSDictionary *result = [NSDictionary
> dictionaryWithDictionary:(NSDictionary *)prefs];
>        //take care of the low-level memory issue
>        CFRelease(prefs);
>        return result;
> }

Yep, this fixes the leak. Your Copy is balanced with a Release and all is well.

However, just to be really nitpicky, I'll point out that it's a bit
ugly. You build a new dictionary from the old one, then throw the old
one away. Better to just keep the old one around with something like
this:

return [(id)prefs autorelease];

Or if you're in a garbage collected environment:

return NSMakeCollectable(prefs);

(And you can do both if you're writing dual-mode code.)

Mike
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to