AssemblyScript array access causing Memory Out of Bounds

0

I am trying to access elements of an Array<u32> in AssemblyScript.

I initialize and fill my arrays as follows:

let totalSize: u32 = 512;

let leaderPtsX: u32[];
let leaderPtsY: u32[];
let leaderPtsType: u32[];

export function createPoints(): void {
  leaderPtsX = new Array<u32>();
  leaderPtsY = new Array<u32>();
  leaderPtsType = new Array<u32>();
  for (let i: u32 = 0; i < VP_COUNT; i++) {
    let x = <u32>(Math.random() * totalSize);
    let y = <u32>(Math.random() * totalSize);
    leaderPtsType.push(Math.random() > 0.5 ? 0xffffffff : 0xff00ff00);
    leaderPtsX.push(x);
    leaderPtsY.push(y);
  }
}

I have a function which needs to access the aforementioned arrays as so:

function getClosestNeighbourType(x: u32, y: u32): u32 {
  let minDistance = 5000;
  let minDistanceType = -1;
  for (let i = 0; i < VP_COUNT; i++) {
    let dist =
      Math.abs(x - unchecked(leaderPtsX[i])) +
      Math.abs(y - unchecked(leaderPtsY[i]));
    if (dist < minDistance) {
      minDistance = <u32>dist;
      minDistanceType = unchecked(leaderPtsType[i]);
    }
  }
  return <u32>minDistanceType;
}

But on compiling and running this in the browser i get the following console error:

array.ts:104 Uncaught RuntimeError: memory access out of bounds
    at ~lib/array/Array<u32>#__unchecked_get (wasm-function[29]:0x1b4a)
    at assembly/index/getClosestNeighbourType (wasm-function[30]:0x1b84)

I have tried lots of different methods and gone through a lot of documentation but no dice. Any help or links appreciated.

Thanks!

arrays
assemblyscript
asked on Stack Overflow May 16, 2020 by psychnaut • edited May 16, 2020 by psychnaut

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0