问题描述
假设可可应用程序必须在退出之前执行一些至关重要的目标操作(由于任何原因,崩溃或退出)。是否可以确保响应每个崩溃事件执行任意的目标c代码? ( SIGINT
, SIGBUS
, SIGSEGV
等,忽略uncatchables( SIGSTOP
, SIGKILL
,闪电等))
For example: let's say that for the app to operate, it absolutely must change the value of some mutable system-wide configuration variable X. On launch, the app takes a snapshot of X's current state and then modifies it. When the app exits successfully, it just restores X to the stashed, original value before terminating. My question is this: is it possible to ensure that X gets restored even if the app crashes?
The short answer is no.
A longer answer is still no, but you can minimise the opportunity for not being able to reset you system-wide mutable configuration variable. In outline:
- On startup have your application spawn a faceless background process;
- The faceless background process should mutate your variable and then wait (as in
wait(2)
& friends) till its parent expires; - On expiry of its parent it resets the variable and then expires itself.
Your faceless background app should be short and simple, and therefore hopefully robust. It's far from foolproof, or safe against a user with infanticide on their minds, but it narrows the opportunities for thwarting the reset of your variable.
这篇关于在Cocoa应用程序退出之前确保Objective-C代码执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!