CFAutoRelease() -like behavior on iOS6

一世执手 提交于 2019-12-12 19:07:12

问题


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?
}

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/19229478/cfautorelease-like-behavior-on-ios6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!