gpt4 book ai didi

ios - CFBridgeRetained 但只有 CFReleased 的 ARC 对象会发生什么?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:18 26 4
gpt4 key购买 nike

假设仍然有未完成的强引用,这是否会导致泄漏,因为没有人(无论是 ARC 还是我)都不再管理该对象?

 CFTypeRef cf_object = CFBridgingRetain(arc_object);
// do stuff
CFRelease(cf_object);

最佳答案

为了回答您的问题,上面的代码看起来不错。在执行 CF CreateRetainCopy 时,需要执行 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,它就会被释放。

DocumentationCFBridgingRelease

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com