gpt4 book ai didi

ios - 什么时候发布核心基础或核心图形对象?

转载 作者:行者123 更新时间:2023-11-29 11:36:36 27 4
gpt4 key购买 nike

我在 iOS 10 和 iOS 11 中使用核心图形和核心基础对象。我应该什么时候发布它?或者它会由 ARC 自动处理,我不应该费心去释放它吗?

原因是因为如果我释放一个CGDataProviderRef 应用程序崩溃 使用 CGDataProviderRelease(inputPDFDataProvider):

//file ref
CFURLRef pdfOutputURL = ( CFURLRef)CFBridgingRetain([NSURL fileURLWithPath:pdfOutputPath]);
CFDataRef inputPDFDataRef = (__bridge CFDataRef)inputFileData;
CGDataProviderRef inputPDFDataProvider = CGDataProviderCreateWithCFData(inputPDFDataRef);
CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithProvider(inputPDFDataProvider);
numberOfPages = CGPDFDocumentGetNumberOfPages(pdfRef);

// Release Core Graphics and Core Foundation Object
CGPDFDocumentRelease(pdfRef);
// CGDataProviderRelease(inputPDFDataProvider); **// CRASHES When releasing this CoreGraphics object**
CFRelease(inputPDFDataRef);
CFRelease(pdfOutputURL);

最佳答案

与手动存储管理一样,所有权是决定性因素。来自 Apple docs :

  • If you create an object (either directly or by making a copy of another object—see The Create Rule), you own it.
  • If you get an object from somewhere else, you do not own it. If you want to prevent it being disposed of, you must add yourself as an owner (using CFRetain).
  • If you are an owner of an object, you must relinquish ownership when you have finished using it (using CFRelease).

当且仅当您是对象的所有者时,您才必须释放该对象。

但是您可以使用 CFBridgingRelease 将所有权转让给 ARC .如果将此函数的结果分配给 Objective C 变量,则您不再拥有参数中的对象。

对于您的情况:您是 inputPDFDataProvider 的所有者,因为 CGDataProviderCreateWithCFData 匹配 Create Rule :

  • Object-creation functions that have “Create” embedded in the name;
  • Object-duplication functions that have “Copy” embedded in the name.

因此,CGDataProviderRelease 是正确的。但是,inputFileData 的内存管理可能存在错误。

关于ios - 什么时候发布核心基础或核心图形对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48861978/

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