Here is the code:
package;
class Main {
public static function main() {
System.init({}, function(){}); //c++ does not without this line in clean new build...
var s = 'test';
trace(s.substr(0, 1) + s.substr(1) + '_');
}
}
It works on most platforms and configurations but crashes in windows when using Visual C++ 2015 with /O2 (default for Kha project) or /Ox optimizations.
It crashes somewhere inside memcpy, which is called from String + operator. Not sure which of the two because adding logging between them fixes the problem
This is generated c++
HXLINE( 6) ::Dynamic _hx_tmp = ::haxe::Log_obj::trace;
HXDLIN( 6) ::String _hx_tmp1 = HX_("test",52,c8,f9,4c).substr((int)0,(int)1);
HXDLIN( 6) ::String _hx_tmp2 = ((_hx_tmp1 + HX_("test",52,c8,f9,4c).substr((int)1,null())) + HX_("_",5f,00,00,00)); // crashes here
HXDLIN( 6) _hx_tmp(_hx_tmp2,hx::SourceInfo(HX_("Main.hx",05,5c,7e,08),6,HX_("Main",59,64,2f,33),HX_("main",39,38,56,48)));
I've tried to run similar code with plain hxcpp without Kha and it worked as expected, but I'm not aware of how to change c++ optimization options with hxcpp. I'm not even sure if I built debug or release...
PS Added some logging to String class:
//added this to String::operator+
Kore::log(Kore::LogLevel::Info, "%i %i %i", inRHS.length, inRHS.__s, *((int*)(&inRHS.__s)+1));
//added this to String::substr() (result is temp variable that stores the result of substr before returning it)
Kore::log(Kore::LogLevel::Info, "%i %i", result.length, result.__s );
and the output was:
3 50990268 - result of substr
20315128 3 50990268 - received by operator+ (pointer moved 4 bytes backwards)
This looks like Visual C++ compiler bug.