Re: Toll Free Garbage
Re: Toll Free Garbage
- Subject: Re: Toll Free Garbage
- From: "Julien Jalon" <email@hidden>
- Date: Mon, 22 Dec 2008 10:56:16 +0100
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
<email@hidden>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 (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
>
_______________________________________________
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