开发者

How to handle exit() in NSOperation

开发者 https://www.devze.com 2023-02-22 03:54 出处:网络
I have a C s开发者_Go百科ource 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 subcl

I have a C s开发者_Go百科ource 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.


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);
    }
}


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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消