0

I am writing a node.js addon and I am having problems with implementing a clone method that returns a copy of the object.

var p = new Point(1, -2);
var copyOfP = p.clone();

Here's my implementation, which is not correct as it does not call the destructor when it has no handles:

static v8::Handle<v8::Value> Clone(const v8::Arguments& args)
{
    HandleScope scope;

    Point* in = ObjectWrap::Unwrap<Point>(args.This());
    Point* out = new Point(in->x_, in->y_); 

    out->Wrap(args.Holder());
    return args.Holder();
}
ZachB
  • 13,051
  • 4
  • 61
  • 89
user3199023
  • 433
  • 2
  • 8

1 Answers1

0

It has been some time ago when I was on node add ons. However from that time this should work. You also may consider this to see object wrapping in action.

Generally I must admit that I did not managed the destructor of a class to be called. Could be that this is a node/v8 related problem.

bodokaiser
  • 15,122
  • 22
  • 97
  • 140
  • I am not sure at the moment. If I create the object using a new operator, i.e. new Point(), I can see that it calls the destructor over if I use p.clone(), it does not free. There is a V8 flag to indicate one is using external memory, which factors in as I am actually creating memory on a GPU. – user3199023 Feb 26 '14 at 19:25
  • Why do you not use Point::New to directly get a v8 object? Cannot help on the external memory issue :( – bodokaiser Feb 26 '14 at 21:08
  • About your last sentence: http://code.google.com/p/v8-juice/wiki/CommentaryOnV8#No_GC_guaranty – jonathancardoso Apr 22 '14 at 18:13