Maybe it is not as bad as I thought. I missed a bit of a sentence in the V8
embedder's guide:
When the destructor, HandleScope::~HandleScope, is called the handle scope
> is deleted. Objects referred to by handles within the deleted handle scope
> are eligible for removal in the next garbage collection *if there are no
> other references to them*.
I *think* this means that because fs.writeFile() has a reference to the
Buffer, it will not be garbage collected, and the memory held on to.
Andy
On Thursday, 13 August 2015 23:09:39 UTC+1, Andy C wrote:
>
> 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/e6575a48-fb38-4a92-ade5-dd5d0ab2470e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.