Re: Get list of possible applications
Re: Get list of possible applications
- Subject: Re: Get list of possible applications
- From: Gerd Knops <email@hidden>
- Date: Sun, 20 Apr 2008 10:15:28 -0500
On Apr 19, 2008, at 11:33 PM, Chris Hanson wrote:
On Apr 19, 2008, at 9:55 AM, Gerd Knops wrote:
This returns an array with URLs of applicable applications for a
file of a given URL:
- (NSArray *)applicationsForURL:(NSURL *)url {
return (NSArray
*)LSCopyApplicationURLsForURL((CFURLRef)url,kLSRolesAll);
}
It also leaks -- even if you're running under Objective-C garbage
collection -- because LSCopyApplicationURLsForURL has "copy" in its
name and therefore follows the Core Foundation "copy" rule.
Sorry, careless copying from a larger context and failure to proof
read... Of course Chris is right.
Gerd
The proper form for the above method, if you aren't using Objective-
C garbage collection, is:
- (NSArray *)applicationsForURL:(NSURL *)url {
return [(NSArray *)LSCopyApplicationURLsForURL((CFURLRef)url,
kLSRolesAll) autorelease];
}
This ensures that the returned NSArray will be autoreleased, instead
of leaked.
If you're using -- or might use -- Objective-C garbage collection,
you'll want to replace the cast to (NSArray *) with NSMakeColectable
like this:
- (NSArray *)applicationsForURL:(NSURL *)url {
return
[NSMakeCollectable(LSCopyApplicationURLsForURL((CFURLRef)url,
kLSRolesAll)) autorelease];
}
The NSMakeCollectable ensures that the CFArrayRef returned by
LSCopyApplicationURLsForURL will have its lifetime managed by the
collector if GC is on, since -autorelease has no effect when GC is
on. This is also correct for non-GC, since NSMakeCollectable when
GC is off is effectively just a cast from (CFTypeRef) to (id).
-- Chris
_______________________________________________
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