How to catch a segmentation fault in hxcpp?

136 views
Skip to first unread message

Víctor R. Escobar

unread,
Dec 12, 2015, 7:33:58 AM12/12/15
to Haxe
Hello,

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 Haxe/C++? Note that in Neko or javascript that is not a problem.


P.S: I posted the same question in stack overflow, hoping that in the future it is easier to find it by googling it (and also more visibility). Link: http://stackoverflow.com/questions/34239849/hxcpp-how-to-catch-a-c-segmentation-fault

Hugh

unread,
Dec 13, 2015, 11:36:21 PM12/13/15
to Haxe
Hi,
The hxcpp target is not really set up to work with native errors/signals, so there is no easy way.

You can add the define HXCPP_CHECK_POINTER, which will explicitly check for NULL on a lot of places.  (This is always on in debug mode)

You can also set the critical error handler, __hxcpp_set_critical_error_handler, like:

untyped __global__.__hxcpp_set_critical_error_handler( function(err) { trace("Error : "+ err); throw err; } );

This will convert certain detectable error into exceptions that can be thrown/caught.

If you use a native debugger, you might be able to work out what is causing the crash and insert your own explicit check.

Hugh
Reply all
Reply to author
Forward
0 new messages