I had the same problem, and here is the way you handle it:
First, you will want to create a callback for when your timer handle gets closed:
void on_timer_close_complete(uv_handle_t* handle)
{
free(handle);
}
Notice that the handle will be freed in the callback.
When you go to stop and free your timer, you would do it like this:
uv_timer_stop(pTimerHandle);
uv_close((uv_handle_t *)pTimerHandle,on_timer_close_complete);
The part that is not intuitive is the uv_close()
call. I don't think I have seen it anywhere in the documentation. I was only able to figure this out by following the source.
This way, libuv will execute your callback when it is done with the timer, and then you can free it safely within that callback.