COMMUNITY

CPP target crashes without any error messages

How to detect null access on cpp target. (multithreading)

For example:

public static var simpleMap:Map<String, Dynamic>;

static public function main() {
	//simpleMap = new Map<String, Dynamic>();

	sys.thread.Thread.create(() -> {
		simpleMap.set("1", {
			example : null
		});
	});

	trace("OK");
	while(true)
	{

	}
}

There is a similar situation in my application, but I cannot find it because exception info not available.

1 Like

IIRC null access is not checked in release mode by default. Either add -D HXCPP_CHECK_POINTER define or build in debug with --debug.
See this doc for other flags: hxcpp/Defines.md at 531303dbf0fef34c2788b3a6be7372a70c77d919 · HaxeFoundation/hxcpp · GitHub

I updated the example, null exception does not show when it is a multi threading.

public static var simpleMap:Map<String, Dynamic>;

static public function main() {
	//simpleMap = new Map<String, Dynamic>();

	sys.thread.Thread.create(() -> {
		simpleMap.set("1", {
			example : null
		});
	});

	trace("OK");
	while(true)
	{

	}
}

Non-Multi Threaded Source Code:

class Main {
    public static var simpleMap:Map<String, Dynamic>;

    static public function main() {
        //simpleMap = new Map<String, Dynamic>();

        simpleMap.set("1", {
            example : null
        });

        trace("OK");
        while(true)
        {

        }
    }
}

Output:

Called from hxcpp::__hxcpp_main
Called from Main::main Main.hx line 7
Error : Null Object Reference


Multi Threaded Source Code:

class Main {
    public static var simpleMap:Map<String, Dynamic>;

    static public function main() {
        //simpleMap = new Map<String, Dynamic>();
        
        sys.thread.Thread.create(() -> {
            simpleMap.set("1", {
                example : null
            });
        });
        
        trace("OK");
        while(true)
        {

        }
    }
}

*Crashing without any output.