Under GC, retain/release is not the same as CFRetain/CFRelease
(retain/release does nothing but CFRetain/CFRelease ensures that the object
is kept around even if the GC might want to finalize it).
The general idea is then that you will have to "cast" the CFRetain at the
same time you cast the CFType:
1) cast a CFType+CFRetain to a NSObject+retain: use NSMakeCollectable()
2) cast a CFType+CFRetain to a __strong CFType+retain: use
CFMakeCollectable()

Note that you can only cast a CFRetain to "GC/retain" if the object was
allocated using the default CFAllocator (kCFDefaultAllocator or NULL)

Also, pair only CFRetains with CFReleases and retains with releases.

Examples:

NSString* s = NSMakeCollectable(CFStringCreateXXX(NULL, ...));

[...]

[s release]; // or nothing if you build only for GC

__strong CFStringRef s =
(CFStringRef)CFMakeCollectable(CFStringCreateXXX(NULL, ...));

[...]

[(id)s release]; // or nothing if you build only for  GC


CFStringRef s = CFStringCreateXXX(NULL, ...);

[...]

CFRelease(s); // required in GC and in no-GC

NSString* s = [NSString stringWithXXX];

CFRetain((CFTypeRef)s); // s will be kept around until CFRelease, in GC and
no-GC

[...]

CFRelease((CFTypeRef)s); // required in GC and in no-GC

-- 
Julien

On Mon, Dec 22, 2008 at 8:40 AM, Gerriet M. Denkmann
<gerr...@mdenkmann.de>wrote:

>
> Assume some plug-in, which must work with garbage collection on or off.
> Assume further, that there is a method (not under our control):
>
> - (NSString *)copySomething;
>
> And that this method returns either an NSString* or a toll free bridged
> CFStringRef, of which we are the owner (because the name contains "copy").
>
> If we do:
> NSString *s = [ someObject copySomething];
> // do something with "s"
> [s release];
>
> it will work without garbage collection, but leak otherwise in the case of
> a CFStringRef.
> How to get rid of "s" ?
> Simply calling CFRelease() is no good, because it might be an NSString* on
> which someone has done a CFRetain().
>
>
> Kind regards,
>
> Gerriet.
>
> _______________________________________________
>
> 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/jjalon%40gmail.com
>
> This email sent to jja...@gmail.com
>
_______________________________________________

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