Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I have a method that creates an ABRecordRef, sets its properties and returns the ref.

I'm encountering a crash when I use CFAutoRelease because I need to support iOS <7. how would I go about to properly releasing this?

-(ABRecordRef) myRecord{
 ABRecordRef newRecord = ABPersonCreate();
//some setting here
return CFAutoRelease(newRecord); //how to release here?
}
share|improve this question
1  
There is no CFAutoRelease. – Andreas Ley Oct 7 '13 at 16:13
3  
Apparently there is, in 10.9 and iOS 7. (I didn't know, either.) – Wevah Oct 7 '13 at 16:50
    
TIL. It's not documented yet, but it is mentioned in the iOS 7 API diffs: developer.apple.com/library/ios/releasenotes/General/… – Peter Hosey Oct 8 '13 at 1:01
    
See Mike Ash's most recent blog post on this very topic. – uchuugaka May 10 '14 at 8:57

2 Answers 2

up vote 4 down vote accepted

For CoreFoundation references, I actually wouldn't release that reference in your myRecord method. Instead I would define the interface such that the caller of myRecord owns the reference and is responsible for releasing it.

share|improve this answer
3  
Make sure to name the method correctly. In this case it should be something like newRecord. By beginning with new, you are making it clear that the object has a retain that must be balanced. – Rob Napier Oct 7 '13 at 19:05
2  
That's true, you should try to follow the 'Create Rule' when working with CoreFoundation. I would actually name the method `CreateRecord' or 'myRecordCreate'. Here's a link to CoreFoundation's Ownership Policy which has more information including the 'Create Rule'. developer.apple.com/library/mac/documentation/corefoundation/… – Jonathan Arbogast Oct 7 '13 at 20:33
    
You might be able to cast to id and call -autorelease on it, too. (There's another question here with that as the accepted answer.) – Wevah Oct 8 '13 at 7:24
    
…Oh right, that won't work under ARC. – Wevah Oct 8 '13 at 7:26

You should be able to build an own autorelease function for Core Foundation objects like this:

inline CFTypeRef MyAutorelease(CFTypeRef obj) { 
    id __autoreleasing result = CFBridgingRelease(obj); 
    return (__bridge CFTypeRef)result; 
}

With __autoreleasing, you force the object to end up in the autorelease pool.

share|improve this answer
    
Mike Ash has as always shown the way. – uchuugaka May 10 '14 at 8:58
    
Actually, the way Mike has shown does not use __autoreleasing. The code above originates from my comment on his blog post. – Tammo Freese May 12 '14 at 9:34
    
Ah. I guess you wrote after I read his. Yours and his are different style. Your follow up question is a good one – uchuugaka May 12 '14 at 12:49
    
@TammoFreese This code crashes when using Optimization Level -Os. Any idea why? – NSAddict Jun 27 '14 at 20:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.