Skip to content

Commit

Permalink
src: add HandleScope in HandleWrap::OnClose
Browse files Browse the repository at this point in the history
Fixes a 4 byte leak on handles closing. AKA The Walmart leak.

MakeCallback doesn't have a HandleScope. That means the callers scope
will retain ownership of created handles from MakeCallback and related.
There is by default a wrapping HandleScope before uv_run, if the caller
doesn't have a HandleScope on the stack the global will take ownership
which won't be reaped until the uv loop exits.

If a uv callback is fired, and there is no enclosing HandleScope in the
cb, you will appear to leak 4-bytes for every invocation. Take heed.

cc @hueniverse
  • Loading branch information
tjfontaine committed Nov 12, 2013
1 parent ac799ba commit 16934d9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/handle_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ HandleWrap::~HandleWrap() {


void HandleWrap::OnClose(uv_handle_t* handle) {
HandleScope scope;

HandleWrap* wrap = static_cast<HandleWrap*>(handle->data);

// The wrap object should still be there.
Expand Down
11 changes: 11 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,17 @@ node_module_struct* get_builtin_module(const char *name);
*/
NODE_EXTERN void AtExit(void (*cb)(void* arg), void* arg = 0);

/*
* MakeCallback doesn't have a HandleScope. That means the callers scope
* will retain ownership of created handles from MakeCallback and related.
* There is by default a wrapping HandleScope before uv_run, if the caller
* doesn't have a HandleScope on the stack the global will take ownership
* which won't be reaped until the uv loop exits.
*
* If a uv callback is fired, and there is no enclosing HandleScope in the
* cb, you will appear to leak 4-bytes for every invocation. Take heed.
*/

NODE_EXTERN void SetErrno(uv_err_t err);
NODE_EXTERN v8::Handle<v8::Value>
MakeCallback(const v8::Handle<v8::Object> object,
Expand Down

0 comments on commit 16934d9

Please sign in to comment.