在Cocoa应用程序退出之前确保Objective-C代码执行 [英] Ensure Objective-C Code Execution Before Cocoa Application Exits

查看:223
本文介绍了在Cocoa应用程序退出之前确保Objective-C代码执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设可可应用程序必须在退出之前执行一些至关重要的目标操作(由于任何原因,崩溃或退出)。



是否可以确保响应每个崩溃事件执行任意的目标c代码? ( SIGINT SIGBUS SIGSEGV 等,忽略uncatchables( SIGSTOP SIGKILL ,闪电等))







这将有助于了解你的钩子是怎么做的。




例如:让我们说,对于应用程序的操作,它绝对必须改变一些可变系统范围的配置变量X的值。在启动时,应用程序拍摄X的快照当前状态,然后修改它。当应用程序成功退出时,它只会将X恢复到隐藏的原始值,然后终止。我的问题是这样:是否可以确保即使应用程序崩溃,X也会恢复?

解决方案



更长的答案仍然是否,但是您可以最大限度地减少无法重置系统范围的可变配置变量的机会。大纲中:




  • 启动时,您的应用程序产生了一个无面的后台进程;

  • 进程应该改变你的变量,然后等待( wait(2)和amp; friends),直到它的父expires;




您的匿名后台应用程序应该简短而简单,因此希望功能强大。它远不是万无一失,或者对于在他们的头脑上有杀婴的用户是安全的,但它缩小了阻止您的变量重置的机会。


Assume that a cocoa app must execute some vitally important objective-c operation before it exits (for any reason—crash or quit).

Is it possible to ensure the execution of arbitrary objective-c code in response to every crash event? (SIGINT, SIGBUS, SIGSEGV, etc, ignoring the "uncatchables" (SIGSTOP, SIGKILL, bolt of lightning, etc.))


It would be helpful to know what your hooks are trying to do.

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代码执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆