3

I Have the following situation that I don't understand. I have an app where from NodeJS I'm calling a C++ function using Nan. The code on the C++ side is as follows:

#include <nan.h>
#include <iostream>

using namespace std;
using namespace Nan;
using namespace v8;

//
//  The function that we are going to call from NodeJS
//
NAN_METHOD(Combine)
{
    char str[80];

    strcpy (str,"these ");
    strcat (str,"strings ");
    strcat (str,"are ");
    strcat (str,"concatenated.");

    //
    //  Send the buffer back to NodeJS with the result of our calculation.
    //
    info
    .GetReturnValue()
    .Set(
        NewBuffer((char *) str, 80)
    .ToLocalChecked());
}

//
//  The constructor
//
NAN_MODULE_INIT(Init)
{
    //
    //  Expose the method or methods to NodeJS
    //
    Nan::Set(
        target,
        New<String>("combine").ToLocalChecked(),
        GetFunction(New<FunctionTemplate>(Combine)).ToLocalChecked()
    );
}

//
//  Load the constructor
//
NODE_MODULE(basic_nan, Init)

When I send back to NodeJS my char variable, I get 80 Bytes, but they are full of random values. It looks as if the place where the str variable was pointing got reclaimed before creating a NewBuffer().

My questions

I would like to get an explanation on what is going on, and ideally get a potential solution .

simon-p-r
  • 3,623
  • 2
  • 20
  • 35
David Gatti
  • 3,576
  • 3
  • 33
  • 64

2 Answers2

1

According to the documentation, NewBuffer assumes that the ownership of the data is transferred to the buffer itself.
You char array is not copied at all, it disappears when it goes out if its scope and you are thus incurring in an undefined behavior.
You should rather allocate the array on the dynamic memory and let the buffer takes the ownership of it to solve the problem.
In other terms, use the new operator to allocate the array of chars, pass it to NewBuffer and don't delete it.

skypjack
  • 49,335
  • 19
  • 95
  • 187
1

I think the problem is that char str[80]; will be allocated in the stack and once your Combine method is finished it will be deallocated(popped from the stack) and overwritten with other stuff that gets pushed into the stack.

Declare it as char* str; and allocate it dynamically with str = new char[80];. Then do all those initializations with strcpy and strcat.

  • `malloc`? Really? – skypjack Oct 11 '16 at 17:19
  • @skypjack I was assuming he's using C since I saw all those strcat and strcpy calls. I've never used that Nan library, I had no idea it's C++. No need to downvote. Besides, malloc should work in C++ too. – Tiberius P. Oct 11 '16 at 17:22
  • Tags contain `C++`. Node.js plugins are written in C++. He is invoking member functions on an object named `info` that apparently return `*this` and this is _C++-ish_. Why did you assume the OP is using C? – skypjack Oct 11 '16 at 17:28
  • @skypjack Okay, I removed that dreaded `malloc` example and replaced it with a `new`. Is it nice and tidy for you now? – Tiberius P. Oct 11 '16 at 17:32