简体   繁体   中英

hxcpp how to catch a C++ segmentation fault?

I am writing a command line tool in haxe (a mini lisp calculator :D) at this point of time in the project I cannot prevent the user to make an invalid call to a nonexisting function or use false arguments (it would require additional wrappers), so I though on the pythonic approach: let it crash and capture the error. However It seems I cannot catch in any way a C++ segmentation fault from haxe.

Example:

hxlisp => (+ 1 2)
REPL.hx:33: Null Function Pointer
hxlisp => (hello)
make: *** [cpp] Segmentation fault: 11

How does the code look like?

public function loop() {
    while (true) {
        try {
            var inp:String = this.input();
            if (inp.length == 0) continue;
            var tree:SExpr = mkSexpr(parse(inp));
            var a = SExpr.List(sexpr_values(tree)[0]);
            var program = eval(a, env.std_env);
            this.output(program);
        } catch(eof:Eof) {
            break;
        } catch(error:Dynamic) {
            trace(error);
        }
    }
}

However it seems there is no way (or I couldn't find it) to catch the error as exception and recover from it. Does anyone have an idea about how much is possible this approach of let it crash and recover from an error in HaxeC++? Note that in Neko or javascript that is not a problem.

No, there is no way to catch and recover from a segmentation fault. A segmentation fault is a hard error: your process has attempted to access invalid memory and the OS is shutting the process down because of it . You must avoid segmentation faults.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM