6

This is part of my stack class. It works fine in flash, but in cpp/windows, the push method doesn't always work. I don't understand C++ well enough to understand why it could be inconsistent.

var arr:Array<T>;
public var length(default, null):Int;

public function new() {
    clear();
}

public inline function clear():Void {
    arr = [];
    length = 0;
}

public inline function push(t:T):Void {
    // Log.trace(arr) -> []
    arr[length++] = t;
    // Log.trace(arr) -> []
}

When push is inlined, something like stack.push(0) does not always change the array, like its true length is fixed at 0 (the length variable does increase, however). Other times, it works fine. If I remove the inline keyword, it works fine all the time.

The generated cpp when inlined (replacing stack.push(0)):

::msg::utils::Stack tmp = this->stack;
::msg::utils::Stack _this = tmp;
Array< int > tmp1 = _this->arr;
int tmp2 = (_this->length)++;
tmp1[tmp2] = (int)0;

And when not inlined (inside push()):

Dynamic tmp = this->arr;
int tmp1 = (this->length)++;
Dynamic tmp2 = t;
hx::IndexRef((tmp).mPtr,tmp1) = tmp2;

Is there something I'm missing that C++ does? Why would the inlined code work most of the time but then not work other times?

Latest haxe, hxcpp, openfl, etc.

Gama11
  • 31,714
  • 9
  • 78
  • 100
MSGhero
  • 411
  • 2
  • 7
  • 5
    This does not look like C++. Is it C++CLI or something like that. – Martin York Apr 28 '15 at 17:46
  • inline shouldn't do anything to functionality. Are you using this in a threaded environment? Threading issues can show up as inconsistent behavior – Jay Apr 28 '15 at 17:48
  • 1
    The first bit is haxe, and the rest is inside a .cpp file, but like I said I don't know cpp. I'm pretty sure this is all running inside one thread since I'm not manually threading anything. – MSGhero Apr 28 '15 at 18:23
  • It looks like you've found a bug in the Haxe compiler. Without knowing the internals of the Haxe implementation, it looks like the inlined version adds the value to a *copy* of the `Array`. Consider posting a bug report to the haxe team. – molbdnilo Apr 28 '15 at 18:30
  • @molbdnilo I wanted to be sure it wasn't something I was doing first, thanks – MSGhero Apr 28 '15 at 18:37
  • 6
    @LokiAstari it's Haxe, targeting CPP. It's a shame stack overflow assumes the most popular tag (cpp) is the most important one for this question. For people following the Haxe tag the question is perfectly reasonable, but I can understand the confusion for those on cpp. I don't think the down-votes are deserved. – Jason O'Neil Apr 29 '15 at 02:31

1 Answers1

2

It's a haxe/hxcpp bug. I reduced it down to the base case, and it turns out that it only works properly now if you set the array variable to a new instance, which I was doing accidentally elsewhere.

https://github.com/HaxeFoundation/haxe/issues/4187

MSGhero
  • 411
  • 2
  • 7