如何在 NSOperation 中处理 exit()

发布于 2024-10-30 15:59:44 字数 331 浏览 3 评论 0原文

我有一个作为应用程序的 C 源代码,我想在我的 iPhone 应用程序中使用它。我认为最好的方法是在 NSOperation 子类中调用该 C 应用程序的 main 方法。

这工作正常,除了 C 应用程序中出现问题并调用 exit()/abort() 的情况,这会导致我的整个 iPHone 应用程序崩溃。

C 代码非常冗长,我不想通过它或以任何方式干扰它。我想知道是否可以绕过 NSOperation 中的那些 exit()/abort() 调用,以便它退出线程并不是整个应用程序。

I have a C source code for as application that I want to use in my iPhone App. I thought the best way of doing it was to call the main method of that C Application in an NSOperation subclass.

This works fine, except for cases where something in the C application goes wrong and an exit()/abort() is called, which takes my whole iPHone App down with it.

The C code is very lengthy and I dont want to go through it or disturb it in any way. I would like to know if I can bypass those exit()/abort() calls in the NSOperation so that it just quits the thread and not the whole application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕巷 2024-11-06 15:59:44

如何使用 -Dexit=my_exit -Dabort=my_abort 选项进行编译并按以下方式实现 my_exit 和 my_abort ?

void my_exit(int status)
{
    if (!/* check thread ID or so forth, and check during calling the NSOperation */) {
        exit(status);
    }
}

How about to compile with -Dexit=my_exit -Dabort=my_abort options and implement my_exit and my_abort as the following?

void my_exit(int status)
{
    if (!/* check thread ID or so forth, and check during calling the NSOperation */) {
        exit(status);
    }
}
静谧幽蓝 2024-11-06 15:59:44

当您自己编译 C 代码时,您可以添加自己的 exit()abort() 函数,C 代码将调用这些函数而不是库中的函数。定义它们以将适当的状态传递给 Obj-C 代码并关闭线程。

对于真正的隔离,当然你想使用 NSTask...

As you are compiling the C code yourself you can add your own exit() and abort() functions, the C code will then call those rather than those in the library. Define them to pass an appropriate status to your Obj-C code and close down the thread.

For real isolation of course you want to use NSTask...

耳钉梦 2024-11-06 15:59:44

我认为在 abort() 之后没有任何实际的方法可以继续执行,可能与 exit() 相同。这两个调用实际上都会终止进程,这意味着原始程序员可能没有费心去清理分配的资源和文件描述符等。因此,即使您终止线程而不是进程,您的应用程序几乎肯定会像筛子一样泄漏每当您覆盖的 exit()/abort() 被调用时。

更严重的是,如果程序调用了 abort(),它可能在运行时检测到一些不可恢复的问题或程序员错误。例如,缓冲区溢出可能会损坏堆。因此,可能无法从这种情况中恢复过来。

恐怕您必须审核每次出现的 exit()abort() 来查看终止线程是否安全。

I don't think there is any practical way to carry on after an abort(), possibly the same for an exit(). Both of these calls actually terminate the process which means that the original programmer probably hasn't bothered to clean up allocated resources and file descriptors etc. So even if you terminate the thread instead of the process, your application will almost certainly leak like a sieve whenever your overridden exit()/abort() is called.

More seriously, if the program has called abort(), it has probably detected some unrecoverable problem with its run time or a programmer error. For instance, a buffer overrun might have corrupted the heap. It may therefore be impossible to recover from the situation.

I'm afraid you'll have to audit every occurrence of exit() and abort() to see if it is even safe to just terminate the thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文