I am writing a C++ addon to node.js. I want to process and bundle a large amount of data into an array of arrays to pass from the C++ process back to js. I've learned that NAN provides a v8 helper class called TypedArrayContents, which lets me directly access a v8 Float32Array through a pointer.

v8::Local<Float32Array> tempHeatmap = v8::Float32Array::New(
        v8::ArrayBuffer::New(isolate, 4 * DIMENSIONS), 0, DIMENSIONS);

Nan::TypedArrayContents<float> dest(tempHeatmap);

for (int i = 0; i < heatmapCollection.size(); i++) {
        v8::Local<Array> sensorDataArray =
            Local<Array>::Cast(parentData->Get(i + 1));

        for (int a = 0; a < nodeCount; a++) {
            sensorDataVec[a] = sensorDataArray->Get(a)->NumberValue();
        }

        for (int j = 0; j < DIMENSIONS; j++) {
            float num = 0.0, denom = 0.0;
            for (int k = 0; k < nodeCount; k++) {
                if (heatmapGrid[j][k] == 0.0) {
                }
                else {

                    num += float(sensorDataVec[k]) / float(heatmapGrid[j][k]);

                    denom += 1 / float(heatmapGrid[j][k]);
                }
            }

            (*dest)[j] = num / denom;

        }
        returnHeatmapCollection->Set(i, tempHeatmap);

    }

returnHeatmapCollection is a v8 array that stores Float32Array's as elements. I can access it from js side no problem.

However, every element inside of returnHeatmapCollection defaults to the post-processing result of the last element. In other words, if I generate 10 tempHeatmap's, the 10th one is loaded into every element in returnHeatmapArray through ->Set().

Why would this occur? It makes infinitely less sense than if it defaulted to the first element. But the fact that the last element overwrites all previous ones would indicate that the every previous element is somehow modified every time I use ->Set. How can this be the case?

0 Answers