如何正确将-fobjc-arc-exceptions标志添加到Xcode中?

huangapple go评论75阅读模式
英文:

How to add -fobjc-arc-exceptions flag correctly into Xcode?

问题

我正在使用 https://github.com/williamFalcon/SwiftTryCatch 作为解决罕见的 NSInternalInconsistencyException 问题的临时方法。

以下是代码片段:

private func safePerformBatchUpdates(_ updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil) {

    SwiftTryCatch.try({
        collectionView.performBatchUpdates(updates, completion: completion)
    }, catch: { (error) in
        print("\(error)")
        
        Crashlytics.crashlytics().record(error: error)
        
        recoverFromPerformBatchUpdatesError()
    }, finally: nil)
}

https://github.com/williamFalcon/SwiftTryCatch 中提到:

已指出,如果不使用 -fobjc-arc-exceptions 标志,这将导致内存泄漏
http://clang.llvm.org/docs/AutomaticReferenceCounting.html#exceptions
因此,ARC 生成的代码在异常情况下默认会泄漏,这对于进程即将立即终止的情况来说没问题。
那些关心从异常中恢复的程序应启用该选项。

我该如何正确添加 -fobjc-arc-exceptions 标志到我的 Xcode 中?

这些是我尝试的步骤:

  1. 在项目窗口左上角选择项目。
  2. 选择目标。
  3. 打开构建阶段窗口。
  4. 选择 "Compile Sources"。

现在,有大约 500 多个源代码文件。我想知道,我应该:

  1. 只向文件 SwiftTryCatch.hSwiftTryCatch.m 添加 -fobjc-arc-exceptions 标志吗?
  2. 只向文件 SwiftTryCatch.hSwiftTryCatch.m 和任何使用 SwiftTryCatch 的 *.swift 文件添加 -fobjc-arc-exceptions 标志吗?
  3. -fobjc-arc-exceptions 标志添加到所有 500+ 个文件中吗?
英文:

I am using https://github.com/williamFalcon/SwiftTryCatch as a workaround for a rare NSInternalInconsistencyException incident.

Here's the code snippet.

private func safePerformBatchUpdates(_ updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil) {

    SwiftTryCatch.try({
        collectionView.performBatchUpdates(updates, completion: completion)
    }, catch: { (error) in
        print("\(error)")
        
        Crashlytics.crashlytics().record(error: error)
        
        recoverFromPerformBatchUpdatesError()
    }, finally: nil)
}

In https://github.com/williamFalcon/SwiftTryCatch, it is mentioning

> It was pointed out that without -fobjc-arc-exceptions flag this will
> lead to memory leaks
> http://clang.llvm.org/docs/AutomaticReferenceCounting.html#exceptions
> Therefore, ARC-generated code leaks by default on exceptions, which
> is just fine if the process is going to be immediately terminated
> anyway. Programs which do care about recovering from exceptions
> should enable the option.

How can I add -fobjc-arc-exceptions flag correctly, into my Xcode?

These are the steps I am trying to do

  1. Select the project at the top left of the project window.
  2. Select the target.
  3. Open the build phases pane.
  4. Select "Compile Sources"

Now, there are around 500+ source code files. I was wondering, should I

  1. Only add -fobjc-arc-exceptions flags, to files SwiftTryCatch.h and SwiftTryCatch.m?
  2. Only add -fobjc-arc-exceptions flags, to files SwiftTryCatch.h, SwiftTryCatch.m and any *.swift files which is using SwiftTryCatch?
  3. Add -fobjc-arc-exceptions flags to all 500+ files?

答案1

得分: 1

你需要使用 -fobjc-arc-exceptions 编译引发异常的文件。因此,你需要重新编译 UIKit(或可能是CoreData),这是不可行的。

还要注意 -fobjc-arc-exceptions 只是帮助预防某些类型的内存泄漏。它并不能使调用免于异常。异常仍然可能导致系统处于未定义状态,这取决于代码编写方式。有时候这种未定义状态不会引起实际问题,但一般来说,从异常中恢复是不可能的。

英文:

You need to compile the file that raises the exception with -fobjc-arc-exceptions. So you would need to recompile UIKit (or probably CoreData), which you cannot do.

Also note that -fobjc-arc-exceptions just helps prevent certain kinds of memory leaks. It does not make the call exception-safe. Exceptions can still leave the system in an undefined state, depending on how the code is writen. Sometimes this undefined state doesn't cause any actual problems, but in general it is not possible to recover from an exception.

huangapple
  • 本文由 发表于 2023年1月9日 18:46:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定