-1

Let me first say that I have very limited knowledge in Objective C programming.

I have a C executable that calls an user-defined function written in Objective C.

Provided that anything could happen in the user code, what would be the best way to safely wrap the code so that the C function returns gracefuly ?

In a C++ wrapper, I would write a function try-catching all exceptions to prevent the C++ exception from propagating into the C code, where the behaviour is not defined by the C standard.

I suppose that I should first prevent any ObjC exception from crossing the language boundaries (i.e. propagate into the C code), so I would write a wrapper like:

void objc_wrap(void) {
    @try {
        obj_entry();
    }
    @catch (NSException *ex) {
        // handle NSException
    }
    @catch (...) {
        // handle the rest
    }
}

However, my limited knowledge of the language does not tell me if there are other mechanisms that would make the user code cross the module boundary and cause undefined exception.

Furthermore, is any initialization/cleanup of the Objective C runtime needed before calling the user function, provided that the entry point is pure C?

Note: Signals and exit() need not be handled, as the wrapper is run in a forked process.

Snaipe
  • 1,191
  • 13
  • 25
  • What do you mean by *make the user code cross the module boundary*? – Caleb Oct 02 '15 at 16:12
  • @Caleb I mean that the only allowed way to return to the C code base is `return`; for instance, unhandled exceptions would effectively cross the boundary and cause undefined behaviour, because it is nowhere specified in the C standard what would happen. Another example would be to `panic!()` in a rust function called from C. – Snaipe Oct 02 '15 at 16:17
  • @zaph edited the question for clarity. Is the problem more understandable? – Snaipe Oct 02 '15 at 16:29

1 Answers1

1

It seems that indeed, @try/catching the user function is the best way to prevent it from ever crossing the language boundaries. As a bonus, any C++ exceptions are also caught by the @catch (...) clause, so there shouldn't be any problem if the Objective-C code calls C++ functions.

Snaipe
  • 1,191
  • 13
  • 25