-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lifetime of a Buffer in a C++ addon #3222
Comments
You need to either keep a reference around to the buffer object or make a copy of the data. It looks like you're already using a |
Ahh, yeah, makes sense. So in my class something like:
And then set it in my function the same way I'm setting the callback:
Am I then correct in assuming that when my class's destructor is called these two persistent references are both released, so there isn't anything else I would need to do? Now that I know where to look, it seems obvious; I'm guessing that I can also make a call to |
|
Ahh, yep, once again, knowing what to look for helps. For future reference, I found the relevant section in Google's v8 Embedder's Guide: https://developers.google.com/v8/embed#handles-and-garbage-collection Specifically where they state:
Thanks for the help! |
@bnoordhuis Upon further reading, I found this comment in the same guide:
Seeing as though I cannot interact with v8 in my uv worker function, do I need to be concerned about my data moving if I store a persistent handle at my class level and then pass a pointer to my data into my worker function? In other words, does the persistent handle guarantee that the pointer I get from |
The memory that the buffer object points to won't move but the object itself can. Rule of thumb: don't interact with node or V8 when on a different thread. That means you shouldn't call e.g. |
Makes sense, so by that logic if I store a pointer to the data (i.e., as a |
Yes, 100% correct. |
@bnoordhuis Awesome, thanks again! |
I'm working on an asynchronous C++ addon that is going to be processing buffers piped in from a stream, and I'm wondering the best way to persist the buffer data from the main event-loop thread into my worker thread. I'm basing much of my design on the way the Node.js zlib module is structured in the sense that I'm exposing a process function of my class that looks something like:
My question is whether or not the
unsigned char *buf
will persist throughout the lifetime of my class, or as I suspect, I will need to create a local copy of the data. I'm wondering if there is a way to force v8 to persist the data until I release it, but I'm just getting into the deep-end (or maybe the shallow end...) of interacting with v8 and would appreciate any guidance on how stuff like this is typically accomplished.The text was updated successfully, but these errors were encountered: