Re: CF autorelease?
Re: CF autorelease?
- Subject: Re: CF autorelease?
- From: John Engelhart <email@hidden>
- Date: Fri, 24 Apr 2009 00:09:56 -0400
On Thu, Apr 23, 2009 at 9:56 PM, Wade Tregaskis <email@hidden> wrote:
> Something I've been using as of late to make CF a little more bearable is
> the cleanup attribute offered by gcc, e.g.:
>
> static inline void _autoreleaseCFArray(CFArrayRef *array) {
>> if (NULL != *array) {
>> CFRelease(*array);
>> }
>> }
>>
>> #define CFAutoreleasedArrayRef
>> __attribute__((cleanup(_autoreleaseCFArray))) CFArrayRef
>>
>
> Unfortunately there's an apparent bug in gcc that requires you to have
> exact type matches, so you can't make e.g. a generic CFTypeRef variant
> (unless your variables really are declared as CFTypeRefs). But still, if
> you're using a particular set of objects a lot, it can save you so much
> work.
>
> Don't be confused by the terminology, though; this'll only "autorelease"
> whatever's assigned to that variable when it goes out of scope. If you
> modify the pointer or clear it, you won't get that autorelease. For simple
> uses this is fine, I find. If your background is predominately C++ you
> might be more comfortable with an "Auto" naming scheme instead.
>
Another caveat with cleanup is that it is not guaranteed to be called. For
this audience, the most likely way this will bite you is when the new zero
cost objective-c exceptions aren't available (i386, ppc) and an exception is
thrown. If zero cost objective-c exceptions are available, then the
compiler arranges to have the cleanup function called during the stack
unwind.
There's enough gotchas to cleanup that I definitely wouldn't count on it for
proper memory management.
_______________________________________________
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