Fix heap corruption issue on windows #623
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In
EIO_AfterWatchPort
, serialport creates av8::ArrayBuffer
usingNan::NewBuffer()
API which createsv8::ArrayBuffer
withArrayBufferCreationMode::kInternalized
mode. This means that v8 engine takes the responsibility of freeing the external buffer passed to thev8::ArrayBuffer
.However
EIO_AfterWatchPort
deletes and frees the buffer at the end of method.When ArrayBuffer object is garbage collected, it tries to free the external buffer passed to it, but since it is already freed, we get heap corruption and the stack looks pretty much like one shown in #617.
The repro for this is inconsistent as it depends on state of memory at the time of GC, but with Microsoft's AppVerifier tool, there is a consistent repro for this.
The fix is to pass
FinalizerCallback
toNan::NewBuffer()
API that will internally create the ArrayBuffer inArrayBufferCreationMode::kExternalized
mode. This means that engine won't take the responsibility of freeing the buffer and when collection of arraybuffer happens, it will call the callback which will free the buffer.Fixes #617, #618