英文:
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 中?
这些是我尝试的步骤:
- 在项目窗口左上角选择项目。
- 选择目标。
- 打开构建阶段窗口。
- 选择 "Compile Sources"。
现在,有大约 500 多个源代码文件。我想知道,我应该:
- 只向文件
SwiftTryCatch.h
和SwiftTryCatch.m
添加-fobjc-arc-exceptions
标志吗? - 只向文件
SwiftTryCatch.h
、SwiftTryCatch.m
和任何使用SwiftTryCatch
的 *.swift 文件添加-fobjc-arc-exceptions
标志吗? - 将
-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
- Select the project at the top left of the project window.
- Select the target.
- Open the build phases pane.
- Select "Compile Sources"
Now, there are around 500+ source code files. I was wondering, should I
- Only add
-fobjc-arc-exceptions
flags, to filesSwiftTryCatch.h
andSwiftTryCatch.m
? - Only add
-fobjc-arc-exceptions
flags, to filesSwiftTryCatch.h
,SwiftTryCatch.m
and any *.swift files which is usingSwiftTryCatch
? - 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论