- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
假设仍然有未完成的强引用,这是否会导致泄漏,因为没有人(无论是 ARC 还是我)都不再管理该对象?
CFTypeRef cf_object = CFBridgingRetain(arc_object);
// do stuff
CFRelease(cf_object);
最佳答案
为了回答您的问题,上面的代码看起来不错。在执行 CF Create
、Retain
或 Copy
时,需要执行 CFRelease
。正如 Rob 所建议的,您还可以查看 CFBridgingRelease
的用法。有关 ARC 更改的更多详细信息,您可以查看此 Raywenderlich tutorial .
根据 apple documentation CFBridgingRetain
,
Casts an Objective-C pointer to a Core Foundation pointer and also transfers ownership to the caller.
CFTypeRef CFBridgingRetain(id X)
Discussion: You use this function to cast an Objective-C object as Core Foundation-style object and take ownership of the object so that you can manage its lifetime. You are responsible for subsequently releasing the object.
在这里您需要释放 ARC 不会处理的 cf_object
。一旦调用 CFRelease
,它就会被释放。
Documentation在 CFBridgingRelease
Moves a non-Objective-C pointer to Objective-C and also transfers ownership to ARC.
id CFBridgingRelease(CFTypeRef X)
Discussion: You use this function to cast a Core Foundation-style object as an Objective-C object and transfer ownership of the object to ARC such that you don’t have to release the object.
请注意,当您使用 CFBridgingRelease
时,它会将所有权转移给 ARC,ARC 将对其进行管理。
例如:-
CFStringRef cfName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *name = (NSString *)CFBridgingRelease(cfName);
ARC 将在这里管理 name
。
关于ios - CFBridgeRetained 但只有 CFReleased 的 ARC 对象会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14175201/
假设仍然有未完成的强引用,这是否会导致泄漏,因为没有人(无论是 ARC 还是我)都不再管理该对象? CFTypeRef cf_object = CFBridgingRetain(arc_object
我是一名优秀的程序员,十分优秀!