Skip navigation.
 
mlRe: Get list of possible applications
FROM : Chris Hanson
DATE : Sun Apr 20 06:33:23 2008

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.

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

Related mailsAuthorDate
mlGet list of possible applications Daniel Apr 19, 18:03
mlRe: Get list of possible applications Gerd Knops Apr 19, 18:55
mlRe: Get list of possible applications Chris Hanson Apr 20, 06:33
mlRe: Get list of possible applications Gerd Knops Apr 20, 17:15
mlRe: Get list of possible applications Daniel Apr 20, 18:49