Possible bug in CFStringCreateByCombiningStrings
I've got a problem with CFStringCreateByCombiningString that i can't solve. Seems to evade explanation:
CFMutableArrayRef filename_arr = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
// insert prefix
int part_prefix = part + 1;
CFStringRef part_str = CFNumberFormatterCreateStringWithValue(kCFAllocatorDefault, format_ref, kCFNumberIntType, &part_prefix);
CFArraySetValueAtIndex(filename_arr, 0, part_str);
// insert prefix separator
CFArraySetValueAtIndex(filename_arr, 1, CFSTR("_"));
// insert name
CFStringRef file_name = mKitManager.CreatePartName(part); // creates and returns a new CFStringRef
CFArraySetValueAtIndex(filename_arr, 2, file_name);
printf("file_name string length: %d\n", CFStringGetLength(file_name));
// add file type extension
CFStringRef file_ext = CFSTR(".aif");
CFArraySetValueAtIndex(filename_arr, 3, file_ext);
// create full file name
CFStringRef full_file_name = CFStringCreateByCombiningStrings(kCFAllocatorDefault, filename_arr, CFSTR(""));
printf("full_file_name string length: %d\n", CFStringGetLength(full_file_name));
Under some conditions the resulting combined string is shorter than it should be. For example, when I insert the name string at index 2 I also check the string length, say that length is 10 characters.
After calling CFStringCreateByCombiningStrings the res开发者_如何学JAVAulting string is sometimes only 3 characters long. The first two elements have been correctly combined but only the first character of the name string has been added and the file extension completely disregarded.
I can reproduce this but have no idea why it happening. A possible bug?
*Edit: Found a workaround for the problem. I think the issue relates to retain counts on the CFStringRef name object but i can't get to the bottom of why it works in some circumstances and not in others.
CFMutableArrayRef filename_arr = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
You have created an empty array.
CFArraySetValueAtIndex(filename_arr, 0, part_str); CFArraySetValueAtIndex(filename_arr, 1, CFSTR("_")); CFArraySetValueAtIndex(filename_arr, 2, file_name); CFArraySetValueAtIndex(filename_arr, 3, file_ext);
And then tried to replace the first through fourth elements of the empty array.
I'm surprised you got as far as that CFStringCreateByCombiningStrings
call. NSMutableArray would have thrown an exception.
The documentation for the CFArraySetValueAtIndex
function says that you can only use it to replace objects already in the array:
The index … must not lie outside the index space of
theArray
(0
toN-1
inclusive, whereN
is the count of the array before the operation).
You want CFArrayInsertValueAtIndex
or CFArrayAppendValue
.
Your total lack of CFRelease()
calls leads me to believe this is a memory management problem. Are you aware that every Core Foundation object returned by a function with "Create" in it returns an owned object? (See: "The Create Rule")
精彩评论