Foundation memory management for CoreFoundation programmers, in brief:
   * the Create rule becomes the "you own all references returned by a
method beginning with new, copy, or alloc"
   * the Get rule becomes "you do not own any references returned by a
method beginning with anything else"
   * CFRetain(CFTypeRef blah) becomes [blah retain].
   * CFRelease(CFTypeRef blah) becomes [blah release] or [blah
autorelease]. See below for more on autoreleasing.

The most common manifestation of this is [[NSBlah alloc] init] falls
under the Create rule, while [NSBlah blah] falls under the Get rule.
You see a lot more use of the Get rule because of Foundation's use of
autorelease pools.

"get" methods in Foundation generally have nothing to do with the Get
rule. Foundation uses a get-prefixed method (like -getBlah:&buffer) to
indicate that a value will be returned by reference in the provided
argument; sometimes you must provide the memory, sometimes it will be
allocated for you – check the documentation to figure out what's going
on.

You are responsible for releasing via -release any owning references
returned to you per the Foundation variation on the Create rule.

You can release those references either directly and immediately via
-release, or indirectly via -autorelease, at which point the topmost
autorelease pool takes ownership of the reference from you. The
autorelease pool stack is generally popped at the end of each cycle
through the event loop.

If you are not using a Cocoa event loop, you are responsible for
setting up an autorelease pool and draining the pool when you are done
with it.

Sending the pool a -drain message (available in Mac OS X 10.4 and
later) is preferable to sending it a release message, because drain
will trigger a garbage collection if necessary in a garbage-collected
environment (otherwise functioning as -release in a reference-counted
environment), while -release is purely a no-op in a garbage-collected
environment.

—Jeremy

On Thu, Apr 16, 2009 at 11:57 AM, Steve Mills <smi...@multi-ad.com> wrote:
> On Apr 16, 2009, at 10:44:07, Jesper Storm Bache wrote:
>
>> You'll want to read Cocoa memory management:
>>
>> http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#//apple_ref/doc/uid/20000043
>>
>> The general rule is that if you do *not* create an instance by using [[X
>> alloc] initZZ] then you should *not* call release (because your instance has
>> been auto released).
>
>
> Thanks for the help, everyone. I hope I remember the rules the next time I
> have to deal with Cocoa.
>
> _________________________________________________________
> Steve Mills                              Me: 952-401-6255
> Senior Software Architect                         MultiAd
> smi...@multiad.com                       www.multi-ad.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/jeremyw.sherman%40gmail.com
>
> This email sent to jeremyw.sher...@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