diff --git a/src/async.c b/src/async.c index 09c5cf81..9492ac56 100644 --- a/src/async.c +++ b/src/async.c @@ -45,6 +45,7 @@ static int luv_new_async(lua_State* L) { } data = luv_setup_handle(L, ctx); data->extra = (luv_thread_arg_t*)malloc(sizeof(luv_thread_arg_t)); + data->extra_gc = free; memset(data->extra, 0, sizeof(luv_thread_arg_t)); handle->data = data; luv_check_callback(L, (luv_handle_t*)handle->data, LUV_ASYNC, 1); diff --git a/src/handle.c b/src/handle.c index 97a2f2ca..98fc02b4 100644 --- a/src/handle.c +++ b/src/handle.c @@ -97,8 +97,8 @@ static int luv_close(lua_State* L) { static void luv_handle_free(uv_handle_t* handle) { luv_handle_t* data = (luv_handle_t*)handle->data; if (data) { - if (data->extra) - free(data->extra); + if (data->extra_gc) + data->extra_gc(data->extra); free(data); } free(handle); diff --git a/src/lhandle.c b/src/lhandle.c index c24ae77b..b8e01e90 100644 --- a/src/lhandle.c +++ b/src/lhandle.c @@ -51,6 +51,7 @@ static luv_handle_t* luv_setup_handle(lua_State* L, luv_ctx_t* ctx) { data->callbacks[1] = LUA_NOREF; data->ctx = ctx; data->extra = NULL; + data->extra_gc = NULL; return data; } diff --git a/src/lhandle.h b/src/lhandle.h index 2340c13e..ca469812 100644 --- a/src/lhandle.h +++ b/src/lhandle.h @@ -39,12 +39,15 @@ #define LUV_FS_POLL 1 #define LUV_RESET 1 +typedef int (*luv_handle_extra_gc) (void* ptr); + /* Ref for userdata and event callbacks */ typedef struct { int ref; int callbacks[2]; luv_ctx_t* ctx; void* extra; + luv_handle_extra_gc extra_gc; } luv_handle_t; #ifdef LUV_SOURCE