Hi,
I am writing a C++ module for node. I have a JS callback registered which 
will receive regular callbacks with binary data (audio data).

My C++ to call the callback looks a bit like:

    UniquePersistent<v8::Function> dataCallback = // from somewhere...

    HandleScope scope(isolate);

    const unsigned argc = 1;
    auto buffer = node::Buffer::New(dataSize);
    std::memcpy(node::Buffer::Data(buffer), data.get(), dataSize);

    Local<Value> argv[argc] = { buffer };

    auto fn = Local<Function>::New(isolate, dataCallback);
    auto context = isolate->GetCurrentContext();
    auto global = context->Global();
    fn->Call(global, argc, argv);

At the moment my callback is simply passing the buffer on to 
fs.writeFile(...):

    var fs = require('fs');
    mymodule.dosomething(function(data) {
        fs.writeFile('raw_data', data, { flag: 'a' })
    });

This appears to work, however the lifetime of the buffer object is suspect. 
This seems unsafe, as my understanding is that the Local<Buffer> is going 
to be cleared up when the HandleScope is destroyed after the callback has 
returned. Unfortunately, fs.writeFile() is probably still busy with the 
buffer.

The key would seem to be something to do with Persistent<> and 
UniquePersistent<> but I don't quite grok it.

Any tips, or pointers to examples would be great...


-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/c1959e4b-0780-4a24-81e6-e67eef1e779e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to