如何在 NSOperation 中处理 exit()
我有一个作为应用程序的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如何使用 -Dexit=my_exit -Dabort=my_abort 选项进行编译并按以下方式实现 my_exit 和 my_abort ?
How about to compile with -Dexit=my_exit -Dabort=my_abort options and implement my_exit and my_abort as the following?
当您自己编译 C 代码时,您可以添加自己的
exit()
和abort()
函数,C 代码将调用这些函数而不是库中的函数。定义它们以将适当的状态传递给 Obj-C 代码并关闭线程。对于真正的隔离,当然你想使用
NSTask
...As you are compiling the C code yourself you can add your own
exit()
andabort()
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
...我认为在
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 anexit()
. 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()
andabort()
to see if it is even safe to just terminate the thread.