core foundation create and get Rule

 

Core Foundation functions have names that indicate when you own a returned object. The object-creation functions have “Create” embedded in the name and the object-duplication functions have “Copy” embedded in the name. If you own an object, it is your responsibility to relinquish ownership (using CFRelease) when you have finished with it.

Consider the following examples. The first example shows two create functions associated with CFTimeZone and one associated with CFBundle.

CFTimeZoneRef     CFTimeZoneCreateWithTimeIntervalFromGMT(CFAllocatorRef allocator, CFTimeInterval ti);
CFDictionaryRef   CFTimeZoneCopyAbbreviationDictionary(void);
CFBundleRef CFBundleCreate (CFAllocatorRef allocator, CFURLRef bundleURL);

The first function contains the word "Create" in its name, and it creates a new CFTimeZone object. You own this object, and it is your responsibility to relinquish ownership. The second function contains the word "Copy" in its name, and creates a copy of an attribute of a time zone object. (Note that this is different from getting the attribute itself—see “The Get Rule”.) Again, you own this object, and it is your responsibility to relinquish ownership. The third function contains the word "Create" in its name, but the documentation states that it may return an existing CFBundle. Again, though, you own this object whether or not a new one is actually created. If an existing object is returned, its retain count is incremented so it is your responsibility to relinquish ownership.

The next example may appear to be more complex, but it still follows the same simple rule.

/* from CFBag.h */
CF_EXPORT CFBagRef  CFBagCreate(CFAllocatorRef allocator, const void **values, CFIndex numValues, const CFBagCallBacks *callBacks);
CF_EXPORT CFMutableBagRef   CFBagCreateMutableCopy(CFAllocatorRef allocator, CFIndex capacity, CFBagRef bag);

The CFBag function CFBagCreateMutableCopy has both “Create” and “Copy” in its name. It is a creation function because the function name contains the word “Create”. Note also that the first argument is of type CFAllocatorRef—this serves as a further hint. The “Copy” in this function is a hint that the function takes a CFBagRef argument and produces a duplicate of the object. It also refers to what happens to the element objects of the source collection: they are copied to the newly created bag. The secondary “Copy” and “NoCopy” substrings of function names indicate how objects owned by some source objects are treated—that is, whether they are copied or not.

当你使用core funcation函数创建一个对象的时候:在函数种包含了‘create’或者’copy‘的,创建对象成功,在你使用完后一定要进行内存的释放。用CFRelease(object);

 

get rule

 

If you receive an object from any Core Foundation function other than a creation or copy function—such as a Get function—you do not own it and cannot be certain of the object’s life span. If you want to ensure that such an object is not disposed of while you are using it, you must claim ownership (with the CFRetain function). You are then responsible for relinquishing ownership when you have finished with it.

Consider the CFAttributedStringGetString function, which returns the backing string for an attributed string.

CFStringRef CFAttributedStringGetString (CFAttributedStringRef aStr);

If the attributed string is freed, it relinquishes ownership of the backing string. If the attributed string was the backing string's only owner, then the backing string now has no owners and it is itself freed. If you need to access the backing string after the attributed string has been disposed of, you must claim ownership (using CFRetain)—or make a copy of it. You must then relinquish ownership (using CFRelease) when you have finished with it, otherwise you create a memory leak.

 

 

例子;

static CFStringRef title = NULL:

void SetTitle(CFStringRef newTitle) {

    CFStringRef temp = title;

    title = CFStringCreateCopy(kCFAllocatorDefault , newTitle);

    CFRelease(temp);

}

 

static CFStringRef title = NULL:

void MyFunction(CFDictionary dict, Boolean aFlag) {

    if (!title && !aFlag) { 

        title = (CFStringRef)CFDictionaryGetValue(dict, CFSTR("title"));

        title = CFRetain(title);

    }

    /* Do something with title here. */

    if (aFlag) {

        CFRelease(title);

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值