Repository navigation
How are Ref and Unref supposed to work? - #869
markandrus wants to merge 1 commit into
Conversation
ecb30fd to
0981b54
Compare
0981b54 to
af31099
Compare
There reference count on an object being greater that 0 does not mean that Node.js will keep running, only that the object will not be collected by the GC.
Calling the Unref significantly changes where the object will be freed so I think that is likely the case that where you do the Unref the pointer that is being freed is still live, while if the object is freed later (during shutdown) the pointer is no longer live. What version of Node.js are you using as :RefBase::~RefBase(this=0x0000000107439860) at js_native_api_v8.cc:223:22 does not seem to line up with master. |
|
@mhdawson thanks for taking a look!
OK, thanks. And so Node may still destroy ObjectWrap instances with non-zero reference counts — it just won't do so via GC? Instead, it may happen at when the process is exiting? Or when the addon is being torn down?
I built from source at Sometimes I get different output from |
|
Looking at the code I think what is going on is that although ObjectWrap happens to have the Ref() and Unref() methods due to the hierarchy, calling them has the potential to mess up the ObjectWrap. An ObjectWrap maintains the liveness of a JavaScript Object and the C level structure associated with it so that they have the same lifetime and are destroyed in the correct sequence. Calling Ref or Unref will mess that up such that the C level structure and JavaScript Object no longer share the same lifespan/liveness a which can easily results in crashes. In terms of understanding how Refs/Unrefs work I suggest you create a separate Ref versus using the ObjectWrap. |
|
@markandrus I think we may have resolved this. Is it ok to close? |
|
Going to close, please let us know if that was not the right thing to do. |
|
Sorry for the delay, @mhdawson -- yes, thank you for closing. |
I've landed a little test for ObjectWrap,
Ref, andUnrefin order to try and understand some behavior I'm seeing. Is it expected that failing to callUnrefshould lead to a "pointer being freed was not allocated" error? In the test I've added, un-commenting the call tounrefcauses the test to pass. Is it wrong to expect that Node.js should remain running so long as the reference count is non-zero?