Download Garbage Collection Programming Guide (Retired
Transcript
Garbage Collection API
Core Foundation
Foundation Functions
Various functions have been added.
void *NSAllocateCollectable(NSUInteger size, NSUInteger options)
Allocates size bytes of memory using the given option.
id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
The zone parameter is ignored by NSAllocateObject in GC mode.
id NSMakeCollectable(CFTypeRef cf )
This function is a wrapper for CFMakeCollectable (see Core Foundation Functions (page 49)), but its
return type is id, avoiding the need to cast if you assign the value to a Cocoa object.
This function may be useful when returning Core Foundation objects in code that must support both
garbage-collected and non-garbage-collected environments, as illustrated in the following example.
- (NSString *)description
{
CFStringRef myCFString = CFStringCreate...(...);
return [NSMakeCollectable(myCFString) autorelease];
}
Core Foundation
The behavior of several functions is different under garbage collection. The Core Foundation collection types
(such as CFSet, CFMutableSet, CFDictionary, and CFArray) correctly support the standard “retaining” callbacks
under GC in a way that allows cycles to be recovered, unlike non-GC behavior. Note also that NULL callbacks
will weakly reference objects, but are not done with zeroing memory—you still need to remove objects from
the collection. If you need zeroing weak object behavior, use NSHashTable or NSMapTable instead.
CFMutableArray
Changed semantics when creating with NULL arguments.
CFArrayCreateMutable(NULL, 0, NULL)
References contents weakly, does not zero. You must remove objects from the array.
CFMutableDictionary
Changed semantics when creating with NULL arguments.
Retired Document | 2012-07-23 | Copyright © 2012 Apple Inc. All Rights Reserved.
48