Kotlin, something wrong with class references? : I'm porting [bullet][1] to [kotlin][2] and I wrote all the necessary stuff to run the HelloWorld sample.

I'm in debugging phase right now and I'm having problems with objects references.

At the very begin, I do enter `collideTTpersistentStack` a first time:

 	    fun collideTTpersistentStack(root0: DbvtNode?, root1: DbvtNode?, collider: DbvtTreeCollider) {

        if (root0 != null && root1 != null) {
            var depth = 1
            var treshold = DOUBLE_STACKSIZE - 4

            val element = StkNN(root0, root1)
            if (stkStack.isNotEmpty()) stkStack[0] = element
            else stkStack += element
            stkStack resize DOUBLE_STACKSIZE
            do {
                val p = stkStack[--depth]
                if (depth > treshold) {
                    stkStack resize (stkStack.size * 2)
                    treshold = stkStack.size - 4
                }
                val pa = p.a!!
                val pb = p.b!!
                if (pa === pb) {
                    if (pa.isInternal) {
                        stkStack[depth++] = StkNN(pa.childs[0], pa.childs[0])
                        stkStack[depth++] = StkNN(pa.childs[1], pa.childs[1])
                        stkStack[depth++] = StkNN(pa.childs[0], pa.childs[1])
                    }
                } else if (pa.volume intersect pb.volume)
                    if (pa.isInternal)
                        if (pb.isInternal) {
                            stkStack[depth++] = StkNN(pa.childs[0], pb.childs[0])
                            stkStack[depth++] = StkNN(pa.childs[1], pb.childs[0])
                            stkStack[depth++] = StkNN(pa.childs[0], pb.childs[1])
                            stkStack[depth++] = StkNN(pa.childs[1], pb.childs[1])
                        } else {
                            stkStack[depth++] = StkNN(pa.childs[0], pb)
                            stkStack[depth++] = StkNN(pa.childs[1], pb)
                        }
                    else
                        if (pb.isInternal) {
                            stkStack[depth++] = StkNN(pa, pb.childs[0])
                            stkStack[depth++] = StkNN(pa, pb.childs[1])
                        } else
                            collider.process(pa, pb)
            } while (depth != 0)
        }
}

but since `root0` is `null` I'll quit right away.
At the next time I enter the function, both `root0` and `root1` are valid objects, and their references are the following

    root0 = Dbvt@708
    root1 = Dbvt@656

Then I create the first element to add to `stkStack`, that is still empty and is defined as:

    val stkStack = ArrayList<StkNN>()

`element` is a `StkNN` Class so defined:

    class StkNN(var a: DbvtNode? = null, var b: DbvtNode? = null)

After the insertion, I get:

[![enter image description here][3]][3]

Which makes sense.

`stkStack resize DOUBLE_STACKSIZE` simply create some dummy StkNN instances

Then I enter the `do` and I grab the first element on the `stkStack`, which is basically the element we just inserted:

    p = {Dbvt$StkNN@731}
       a = {DbvtNode@708}
       b = {DbvtNode@656}

then we skip the next `if`, I save the variables `p.a` and `p.b` as immutable checking against nullability

    val pa = p.a!!
    val pb = p.b!!

Both `pa` and `pb` are consistent in terms of references:

    pa = @708
    pb = @656

And now we land directly [here][4]:

                        } else {
                            stkStack[depth++] = StkNN(pa.childs[0], pb)
                            stkStack[depth++] = StkNN(pa.childs[1], pb)
                        }

`depth` now is `0` and `stkStack` contains one element, the one we inserted at the begin, so it should replace it with a new instance

Well, `pa` childs are the following

    pa = {DbvtNode@708} 
     childs = {DbvtNode[2]@748} 
      0 = {DbvtNode@759} 
      1 = {DbvtNode@656} 

But after I jump over the assignments, `stkStack` will contain the following:

    stkStack = {ArrayList@709}  size = 128
     0 = {Dbvt$StkNN@772} 
      a = {DbvtNode@708} // this is wrong, it should be @759
      b = {DbvtNode@656} 
     1 = {Dbvt$StkNN@781} 
      a = {DbvtNode@656} // right
      b = {DbvtNode@656} // right

C++ code uses pointers, and I double check the execution and its `stkStack[0].a` pointer is actually as it should be, that is, it corresponds to the `p.a->childs[0]` pointer

What is happening?

  [1]: http://bulletphysics.org/
  [2]: https://github.com/kotlin-graphics/bullet
  [3]: https://i.stack.imgur.com/ZOrL9.png
  [4]: https://github.com/kotlin-graphics/bullet/blob/master/src/main/kotlin/bullet/collision/broadphaseCollision/Dbvt.kt#L417