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

_______________________________________________

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 [EMAIL PROTECTED]

Reply via email to