Re: A Mac App & helper NSStatusItem - how to share preferences
Re: A Mac App & helper NSStatusItem - how to share preferences
- Subject: Re: A Mac App & helper NSStatusItem - how to share preferences
- From: "Michael Ash" <email@hidden>
- Date: Tue, 30 Dec 2008 19:22:51 -0500
On Tue, Dec 30, 2008 at 4:14 PM, Steve Cronin <email@hidden> 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 (email@hidden)
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:
This email sent to email@hidden