Graphics Programs Reference
In-Depth Information
CFStringRef newUniqueIDString =
CFUUIDCreateString (kCFAllocatorDefault, newUniqueID);
// Use that unique ID to set our item's imageKey
NSString *key = (__bridge NSString *)newUniqueIDString;
[item setImageKey:key];
// Store image in the BNRImageStore with this key
[[BNRImageStore sharedStore] setImage:image
forKey:[item imageKey]];
Notice the use of __bridge in this typecast. To understand what this keyword does, you
must understand how memory is managed for Core Foundation objects.
Core Foundation and toll-free bridging
When a variable that points to an Objective-C object is destroyed, ARC knows that object
has lost an owner. ARC doesn't do this with Core Foundation objects. Thus, when a Core
Foundation object loses a pointer, you must call a function that tells the object to lose an
owner before you lose the pointer. This function is CFRelease .
If you do not call CFRelease before losing a pointer, the pointed-to object still thinks it
has an owner. Losing a pointer to an object before telling it to lose an owner results in a
memory leak: you can no longer access that object, and it still has an owner. Add code to
imagePickerController:didFinishPickingMediaWithInfo: to tell the
objects pointed to by newUniqueIDString and newUniqueID to lose an owner
since these are both local variables that will be destroyed when this method ends.
[[BNRImageStore sharedStore] setImage:image
forKey:[item imageKey]];
CFRelease(newUniqueIDString);
CFRelease(newUniqueID);
[imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:nil];
}
Here are the memory management rules when it comes to Core Foundation objects.
• A variable only owns the object it points to if the function that created the object
has the word Create or Copy in it.
Search WWH ::




Custom Search